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]

Leave a Comment