10. Problem to split the list into two-part.

In this program, we will take a user input as a list and split the list into two-part, the left side all odd values and the right side all even values i.e. split the list with the help of the below-given steps.

Split the list:

Steps to solve the program
  1. Take a list as input and create two empty lists.
  2. Add even and odd elements from the given list into the empty list with the help of for loop.
  3. Combine odd element list with even element list using extend().
  4. Print the output.
				
					#Input list
list1 = [5, 7, 2, 8, 11, 12, 17, 19, 22]

#Creating empty list
even = []
odd = []

for value in list1:
    if value%2 == 0:
        even.append(value)
    else:
        odd.append(value)

#Combining lists
odd.extend(even)

#printing output
print(odd)
				
			

Output :

				
					[5, 7, 11, 17, 19, 2, 8, 12, 22]
				
			

Related Articles

Squares of all even numbers

Get common elements from two lists.

9. Problem to print squares of even numbers from a list.

In this program, we will take a user input as a list and print squares of even numbers from that list with the help of the below-given steps.

Squares of even numbers:

Steps to solve the program
  1. Take a list as input.
  2. Using for loop check whether an element from the list is even or not.
  3. If an element is even then print its square using **n.
				
					#Input list
list1 = [2,4,7,8,5,1,6]

for value in list1:
    if value%2 == 0:
        #Printing output
        print(value,":",value**2)
				
			

Output :

				
					2 : 4
4 : 16
8 : 64
6 : 36
				
			

Related Articles

Combination of 2 elements from the list whose sum is 10.

split the list with even,odd values

8. Problem to find the combination of 2 elements from the list.

In this program, we will take a user input as a list and print the combination of 2 elements whose sum is 10 with the help of the below-given steps.

Combination of 2 elements:

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. Import itertools.
  3. With the help of for loop and itertools.combinations check for combinations from the given list whose sum is 10.
  4. Add such combinations to the empty list.
  5. Print the list to see the combination of 2 elements.
				
					#Importing itertools
import itertools

#Input list
list1 = [2,5,8,5,1,9]

#Creating variable
var = 10

#Creating empty list
list3 = []

for i in range(len(list1)):
    for combi in itertools.combinations(list1,i):
        if sum(combi) == var:
            list3.append(combi)

#Printing output
print(list3)
				
			

Output :

				
					[(2, 8), (5, 5), (1, 9)]
				
			

Related Articles

Remove all duplicate elements

Squares of all even numbers

7. Problem to remove all the duplicate elements from a list.

In this program, we will take a user input as a list & remove all the duplicate elements with the help of the below-given steps.

Remove duplicates from list:

Steps to solve the program
  1. Take a list as input and create an empty list.
  2. With the help of for loop add elements from the given list to the empty list.
  3. If an element repeats then do not add it.
  4. Print the list to see the output.
				
					#Input list
list1 = [5,7,8,2,5,0,7,2]
 
#Creating empty list
list2 = []

for value in list1:
    if value not in list2:
        list2.append(value)
        
#Printing Output
print(list2)

				
			

Output :

				
					[5, 7, 8, 2, 0]
				
			

Related Articles

Differentiate even and odd elements

Combination of 2 elements from the list whose sum is 10.

6. Problem to differentiate even and odd elements from a list.

In this program, we will take a user input as a list and differentiate even and odd elements with the help of the below-given steps.

Differentiate even and odd number in list:

Steps to solve the program
  1. Take a list as input and create two empty lists with names odd and even.
  2. Use for loop to iterate over each element of the given list.
  3. Use the if statement to check whether the element is even or not during iteration.
  4. If even add that element to the even list, if not add it to the odd list to differentiate even and odd numbers.
  5. Print both lists to see the output.
				
					#Input list
og_list = [23,11,78,90,34,55]

#creating empty lists
odd = []
even = []

for value in og_list:
    if value%2 == 0:
        even.append(value)
    else:
        odd.append(value)
        
#printing output
print("Even numbers: ", even)
print("Odd numbers: ", odd)
				
			

Output :

				
					Even numbers:  [78, 90, 34]
Odd numbers:  [23, 11, 55]
				
			

Related Articles

minimum and maximum elements

Remove all duplicate elements

5. Problem to find minimum and maximum elements from a list

In this program, we will take a user input as a list and find the minimum and maximum elements from it with the help of the below-given steps.

Table of Contents

Minimum and maximum element in list:

Method 1 : Sorting and indexing

Steps to solve the program
  1. Take a list as input.
  2. Sort the list using Sort().
  3. Print the maximum value i.e. element with -1 index.
  4. Print the minimum value i.e. element with 0 index.
				
					#Input list
list1 = [23,56,12,89]

#Sorting list
list1.sort()

#Printing output
print("Smallest number: ", list1[0])
print("Largest number: ", list1[-1])
				
			

Output :

				
					Smallest number:  12
Largest number:  89
				
			

Minimum and maximum element in list:

Method 2 : Using in-built function

Steps to solve the program
  1. Take a list as input.
  2. Print the maximum value using max().
  3. Print the minimum value using min().
				
					#Input list
list1 = [23,56,12,89]

#Printing output
print("Smallest number: ", min(list1))
print("Largest number: ", max(list1))
				
			

Output :

				
					Smallest number:  12
Largest number:  89
				
			

Related Articles

Product of all elements

 Differentiate even and odd elements

4. Problem to get product of all elements in a list

In this program, we will take a user input as a list & iterate all the values one by one with the python loop to find the product of all elements in a list with the help of the below-given steps.

Table of Contents

Method 1:  Using for loop

Steps to solve the program
  1. Take a list as input.
  2. Using for loop with the necessary condition find the product of all elements in a list.
				
					#Input list
list1 = [3,6,9,2]

#Creating variable
var = 1

for value in list1:
    var *= value
    
#Printing output
print(var)
				
			

Output :

				
					324
				
			

Method 2 : Using while loop

Steps to solve the program
  1. Take a list as input.
  2. Use while loop with the necessary conditions to find the product of all elements of the list.
				
					#Input list
list1 = [3,6,9,2]

#creating variables
product = 1
count = 0

while count < len(list1):
    product *= list1[count]
    count += 1

#Printing output
print(product)
				
			

Output :

				
					324
				
			

Related Articles

Sum of all elements

Minimum and maximum elements

3. Problem to print the sum of all elements in a list.

In this program, we will take a user input as a list & iterate all the values one by one with the python loop to find the sum of all elements in a list.

Table of Contents

Method 1: Using for loop

Steps to solve the program
  1. Take a list as input.
  2. Use for loop to iterate over every element of the list.
  3. Find and print the sum of all elements of the list using the appropriate condition.
				
					#Input list
list1 = [2,5,8,0,1]

#Creating a variable
var = 0

for value in list1:
    var += value

print(var)
				
			

Output :

				
					
16
				
			

Method 2 : Using while loop

Steps to solve the program
  1. Take a list as input.
  2. Using while loop and necessary condition find and print the sum of all elements of the list.
				
					#Input list
list1 = [2,5,8,0,1]

#Creating variable
count = 0
total = 0

while count < len(list1):
    total += list1[count]
    count += 1

#Printing output
print(total)
				
			

Output :

				
					16
				
			

Method 3 : Using sum() function

Steps to solve the program
  1. Take a list as input.
  2. Use sum() function to find the sum of all the elements of the list.
				
					#Input list
list1 = [2,5,8,0,1]

#Printing output
print(sum(list1))
				
			

Output :

				
					16
				
			

Related Articles

 Combine two lists

Product of all elements

Python Numpy Practice Programs, Exercises

Numpy is one of the most popular Python libraries for data analysis. It’s a library that provides a way to manipulate and process arrays of data in both one and two dimensions.

1). Python program to get the NumPy version

2). Python NumPy program to test whether none of the elements of a given array is zero.
Input : [6,8,3,5]
Output : True

3). Python NumPy program to test a given array element-wise is finite or not.
Input : [6,8,3,np.inf]
Output : [False, False, False, True]

4). Python NumPy program to test element-wise for NaN of a given array.
Input :[2,4,np.nan,7]
Output : [False False True False]

5). Python NumPy program to test element-wise for complex numbers of a given array.
Input :[2,4,6+9j,7]
Output : [False, False, True, False]

6). Python NumPy program to test element-wise for real numbers of a given array.
Input : [2,4,6+9j,7]
Output : [True, True, False, True]

7). Python NumPy program to test whether a number is scalar or not.
Input : 8.9
Output : True

Input : [8.9]
Output : False

8). Python NumPy program to test whether two arrays are element-wise equal.
Input :
[3,8,5]
[3,8,5]
Output : [ True True True]

9). Python NumPy program to create an element-wise comparison (greater than).
Input =
[8,3]
[9,1]
Output : [ False, True]

10). Python NumPy program to create an element-wise comparison (greater equal).
Input =
[6,4]
[6,3]
Output : [ True, True]

11). Python NumPy program to create an element-wise comparison (less than).
Input =
[6,4]
[2,9]
Output : [ False, True]

12). Python Numpy program to create an array with the values 10,34,86,26,56.
Output : [10,34,86,26,56]

13). Python NumPy program to determine the size of the memory occupied by the above array.

14). Python NumPy program to create an array of 3 ones.
Output : [1,1,1]

15). Python NumPy program to create an array of integers between 1-20.

16). Python NumPy program to create an array of even integers between 1-20.

17). Python NumPy program to create an array of odd integers between 1-20.

18). Python NumPy program to create an array of integers between 1-100 by the difference of 6 between them.

19). Python NumPy program to create a 3×3 matrix.

20). Python NumPy program to create a 3×3 identify matrix.

21). Python NumPy program to generate random numbers between 1-2.

22). Python NumPy program to generate an array of 5 random numbers from a standard normal distribution.

23). Python NumPy program to create a vector with values ranging from 1 to 20.
Output : [1,2,3,….,20]

24). Python program to print all the values from the array except 20.
Output : [1,2,3,….,19]

25). Python NumPy program to print all the values from the above array except 1.
Output : [2,3,….,20]

26). Python NumPy program to create a 3×4 matrix.

27). Python NumPy program to create a 2×3 matrix.

28). Write a program to create a 2×2 matrix.

29). Python NumPy program to create a vector with values from 1 to 15 and change the sign of the numbers in the range from 6 to 10.
Output : [1,2,3,4,5,-6,-7,-8,-9,-10,11,12,13,14,15]

30). Python NumPy program to multiply two vectors.
Input =
[4,2,8]
[7,3,5]
Output : [28,6,40]

31). Python NumPy program to create a 2×3 matrix filled with values from 5-10.
Output =
[[ 5 6 7]
[8 9 10]]

32). Python NumPy program to find the shape of the given matrix.
Input =
[[5 6 7]
[8 9 10]]
Output : (2,3)

33). Python NumPy program to reshape the 2×3 matrix into a 2×2 matrix.
Input =
[[5 6 7]
[8 9 10]]
Output =
[[ 5 6]
[7 8]
[9 10]]

34). Python NumPy program to create a 2×2 zero matrix with elements on the main diagonal equal to 7,8.
Output =
[[ 7 0]
[0 8]]

35). Python NumPy program to calculate the sum of all elements in an array.
Input :
[[ 5 3]
[7 4]]
Output : 19

36). Python NumPy program to calculate the sum of each row in an array.
Input =
[[ 5 3]
[7 4]]
Output : [8,11]

37). Python NumPy program to calculate the sum of each column in an array.
Input =
[[ 5 3]
[7 4]]
Output : [12,7]

38). Python program to calculate the inner/dot product of two vectors.
Input =
[[ 5 3]
[7 4]]
Output : 42

39). Python NumPy program to add a vector to each row of a matrix.
Input =
[2,5]
[[ 5 3]
[7 4]]
Output =
[[ 7 8]
[9 9]]

40). Python NumPy program to convert the list into an array.
Input : [[3,0],[6,4]]
Output =
[[ 3 0]
[6 4]]

41). Python NumPy program to convert an array into a list.
Input =
[[ 3 0]
[6 4]]
Output :[[3,0],[6,4]]

42). Python NumPy program to find the missing data in a given array.
Input =
[[ 1 5 Nan]
[8 Nan 9]]
Output =
[[ False False True
False True False]]

43). Python NumPy program to check whether two arrays are element-wise equal.
Input =
[5,3]
[7,8]
Output : [False, False]

44). Python NumPy program to create a one-dimensional array.
Output : [65,90,1,3,5]

45). Python NumPy program to interchange the rows of a matrix.
Input =
[[ 5 6]
[7 8]
[9 10]]
Output =
[[ 5 7 9]
[ 6 8 10]]

46). Python NumPy program to sort an array by row in ascending order.
Input =
[[ 6 5]
[10 9]
[8 7]]
Output =
[[ 5 6]
[9 10]
[7 8]]

47). Python NumPy program to sort a given array by column in ascending order.
Input =
[[ 6 5]
[10 9]
[7 8]]
Output =
[[6 5]
[7 8]
[10 9]]

48). Python NumPy program to extract all numbers from a given array that are greater than a specified number.
Input =
[[ 6 5]
[10 9]
[7 8]]
Output =
Greater than 7
[ 10,9,8]

49). Python NumPy program to replace all numbers in a given array that is equal or greater than a given number.
Input =
[[ 6 5]
[10 9]
[7 8]]
Output =
Replace numbers greater than or equal to 7 with 0
[[ 6 5]
[0 0]
[0 0 ]]

50). Write a program to swap rows of a given array.
Input =
[[ 6 5]
[10 9]
[7 8]]
Output =
[[ 8 7]
[10 9]
[ 6 5]]

51). Python NumPy program to multiply a row of an array by a scalar.
Input =
[[ 6 5]
[10 9]
[7 8]]
Output =
Multiply row 1 by 2
[[ 12 10]
[10 9]
[7 8]]

52). Python NumPy program to add a row of a matrix into another row.
Input =
[[ 6 5]
[10 9]
[7 8]]
Output =
Add row 2 to row 1
[[ 16 14]
[10 9]
[7 8]]

53). Python NumPy program to find the real and imaginary parts of an array of complex numbers.
Input : [6+2j,10+5j]
Output =
Real part- 6,10
Imaginary part- 2,5

54). Python NumPy program to find common elements between two arrays.
Input =
[56,18,28,36]
[76,36,56]
Output : [36,56]

55). Python NumPy program to get the unique elements of an array.
Input : [25,33,10,45,33,10]
Output : [25,33,10,45]

56). Python NumPy program to find the set difference between two arrays. The set will give unique values in array1 that are not in array2.
Input =
[56,18,28,36]
[76,36,56]
Output : [18,28]

57). Python NumPy program to find the union of two arrays. The union will return the values that are in either of the two input arrays.
Input =
[56,18,28,36]
[76,36,56,90]
Output : [56,18,28,36,76,90]

58). Python NumPy program to find the indices of the maximum value of an array.
Input : [76,36,56,90]
Output : 3

59). Python Numpy program to find the indices of the minimum value of an array.
Input : [76,36,56,90]
Output : 1

60). Python Numpy program to change the data type of an array.
Input =
[[ 4 8 7]
[ 2 3 6]]
The data type of the array x is: int32
Output =
[[ 4. 8. 7.]
[ 2. 3. 6.]]
New Type: float64

61). Python NumPy program to find the ith element of an array.
Input =
[[ 4 8 7]
[ 2 3 6]]
Output =
i=5
3

62). Python NumPu program to remove single-dimensional entries from a specified shape.
Input = original shape: (2,1,3)
Output : (2,3)

63). Python NumPy program to convert 1-D arrays as columns into a 2-D array.
Input : (3,8,5),(4,0,7)
Output =
[[ 3 4]
[8 0]
[5 7]]

64). Python NumPy program to split an array of 10 elements into 3 arrays, each of which has 2, 3, and 5 elements.
Input = [ 1,2,3,4,5,6,7,8,9,10]
Output = [array([1,2]), array ([3,4,5]), array ([6,7,8,9,10])]

65). Python NumPy program to get the number of nonzero elements in an array.
Input =
[[ 4 8 0]
[2 0 6]]
Output : 4

66). Python NumPy program to find the median of the matrix.

67). Python NumPy program to find the median of the matrix along the rows.

68). Python NumPy program to find the median of the matrix along the column.

69). Python NumPy program to make an array immutable i.e. read-only.

70). Python NumPy program to print every element of an array.
Input =
[[ 4 8 0]
[2 0 6]]
Output : 4 8 0 2 0 6

71). Python NumPy program to print squares of all the elements of an array.
Input : [2,6,3,1]
Output : [4,36,9,1]

72). Python NumPy program to access an array by column.
Input =
[[ 4 8 0]
[ 2 0 6]]
Output =
1st column:
[4 2]
2nd column:
[8 0]
3rd column:
[0 6]

73). Python NumPy program to convert an array of float values to an array of integer values.
Input =
[[ 6.7 5.2]
[9.4 2.7]]
Output =
[[ 6 5]
[9 2]]

74). Python NumPy to add an extra column to an array.
Input =
[[ 4 8 0]
[2 0 6]]
Output =
[[ 4 8 0 5]
[2 0 6 7 ]]

75). Python NumPy program to replace the negative values in an array with 1.
Input =
[[ 4 8 -5]
[2 -9 6]]
Output =
[[ 4 8 1]
[2 1 6]]

76). Python NumPy program to remove all rows in a NumPy array that contain non-numeric values.
Input =
[[ 4 NaN 8]
[2 4 6]]
Output =
[2 1 6]

77). Python NumPy program to get the magnitude of a vector.
Input : [3 5 1]
Output : 5.91

78). Python NumPy program to count the frequency of unique values.
Input : [ 8 6 7 0 7 8 6 6]
Output =
[[ 0 6 7 8
1 3 2 2]]

79). Python NumPy program to check whether an array is empty.
Input =
[4,8,0]
Output : Array is not empty

80). Python NumPy program to calculate the product of an array.
Input : [3 5 1]
Output : 15

81). Python NumPy program to convert an array into a CSV file.

82). Python NumPy program to access first two columns of a 3-D array.
Input =
[[ 6 5 7]
[10 9 1]
[7 8 2]]
Output = 1 column: [ 6 10 7], 2 column: [5 9 8]

83). Python NumPy program to extract the first and second elements of the first and second rows from a given (3×3) matrix.
Input =
[[ 6 5 7]
[10 9 1]
[7 8 2]]
Output =
[[ 6 5]
[10 9]]

84). Python NumPy program to create an array that represents the rank of each item of a given array.
Input : [6 8 4 9 5 0]
Output : [3 4 1 5 2 0]

85). Python NumPy program to copy data from a given array to another array.

86). Python NumPy program to calculate the sum of all columns of an array
Input =
[[ 6 5 7]
[10 9 1]
[7 8 2]]
Output : [23 22 10]

87). Python NumPy program to calculate averages without NaNs along a row.
Input =
[[ 6 5 nan]
[nan 9 1]
[7 8 2]]
Output : [ 5.5 5 8.5]

88). Python NumPy program to merge two given arrays of identical shape.

89). Python NumPy program to convert a python dictionary to a n-dimensional array.

90). Python NumPy program to convert an array to a data frame with headers.

91). Python NumPy program to create an array and reshape it.

92). Python NumPy program to check whether the dimensions of two arrays are the same or not.

93). Python NumPy program to calculate the sin angle of an array.

94). Python NumPy program to calculate the standard deviation of an array.

95). Python NumPy program to calculate the variance of an array.

96). Python NumPy program to calculate the cos angle of an array.

97). Python NumPy program to calculate the tan angle of an array.

98). Python NumPy program to round off the elements in an array up to two digits.
Input : [4.8757 2.4578]
Output : [4.86 2.45]

99). Python NumPy program to round off the elements in an array using the floor function.
Input : [4.8757 2.4578]
Output : [4 2]

100). Python NumPy program to round off the elements in an array using ceil function.
Input : [4.8757 2.4578]
Output : [5 3]

101). Python NumPy program to convert a tuple into an array.
Input : (5,7,0)
Output : [5 7 0]

102). Python NumPy program to create an array using dtype as complex.
Output : [5.+0.j 4.+0.j]