NumPy Basic: Exercises, Practice, Solution
NumPy Basic [59 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Get NumPy Version & Build Info
Write a Numpy program to get the Numpy version and show the Numpy build configuration.
Click me to see the sample solution
2. Help for NumPy Add Function
Write a NumPy program to get help with the add function.
Click me to see the sample solution
3. Test None are Zero
Write a NumPy program to test whether none of the elements of a given array are zero.
Click me to see the sample solution
4. Test Any Non-Zero
Write a NumPy program to test if any of the elements of a given array are non-zero.
Click me to see the sample solution
5. Test Finiteness of Elements
Write a NumPy program to test a given array element-wise for finiteness (not infinity or not a number).
Click me to see the sample solution
6. Test for Infinity
Write a NumPy program to test elements-wise for positive or negative infinity.
Click me to see the sample solution
7. Test for NaN
Write a NumPy program to test element-wise for NaN of a given array.
Click me to see the sample solution
8. Test for Complex/Real/Scalar
Write a NumPy program to test element-wise for complex numbers, real numbers in a given array. Also test if a given number is of a scalar type or not.
Click me to see the sample solution
9. Test Element-Wise Tolerance Equality
Write a NumPy program to test whether two arrays are element-wise equal within a tolerance.
Click me to see the sample solution
10. Element-Wise Comparison (Greater/Less)
Write a NumPy program to create an element-wise comparison (greater, greater_equal, less and less_equal) of two given arrays.
Click me to see the sample solution
11. Element-Wise Comparison (Equal/Tolerance)
Write a NumPy program to create an element-wise comparison (equal, equal within a tolerance) of two given arrays.
Click me to see the sample solution
12. Array Memory Size
Write a NumPy program to create an array with the values 1, 7, 13, 105 and determine the size of the memory occupied by the array.
Click me to see the sample solution
13. Create Array of Zeros, Ones, Fives
Write a NumPy program to create an array of 10 zeros, 10 ones, and 10 fives.
Click me to see the sample solution
14. Create Array of Integers 30 to 70
Write a NumPy program to create an array of integers from 30 to 70.
Click me to see the sample solution
15. Create Array of Even Integers 30 to 70
Write a NumPy program to create an array of all even integers from 30 to 70.
Click me to see the sample solution
16. Create 3x3 Identity Matrix
Write a NumPy program to create a 3x3 identity matrix.
Click me to see the sample solution
17. Generate Random Number [0,1]
Write a NumPy program to generate a random number between 0 and 1.
Click me to see the sample solution
18. Generate Random Array from Standard Normal
Write a NumPy program to generate an array of 15 random numbers from a standard normal distribution.
Click me to see the sample solution
19. Vector 15-55 Without First/Last Values
Write a NumPy program to create a vector with values ranging from 15 to 55 and print all values except the first and last.
Click me to see the sample solution
20. Create and Iterate 3x4 Array
Write a NumPy program to create a 3X4 array and iterate over it.
Click me to see the sample solution
21. Create Vector of Evenly Spaced Values
Write a NumPy program to create a vector of length 10 with values evenly distributed between 5 and 50.
Click me to see the sample solution
22. Change Sign of Range 9-15
Write a NumPy program to create a vector with values from 0 to 20 and change the sign of the numbers in the range from 9 to 15.
Click me to see the sample solution
23. Vector of Random Integers [0,10]
Write a NumPy program to create a vector of length 5 filled with arbitrary integers from 0 to 10.
Click me to see the sample solution
24. Multiply Two Vectors
Write a NumPy program to multiply the values of two given vectors.
Click me to see the sample solution
25. Create 3x4 Matrix (10 to 21)
Write a NumPy program to create a 3x4 matrix filled with values from 10 to 21.
Click me to see the sample solution
26. Rows & Columns in Matrix
Write a NumPy program to find the number of rows and columns in a given matrix.
Click me to see the sample solution
27. Create 3x3 Identity Matrix
Write a NumPy program to create a 3x3 identity matrix, i.e. the diagonal elements are 1, the rest are 0.
Click me to see the sample solution
28. 10x10 Matrix Border 1, Inside 0
Write a NumPy program to create a 10x10 matrix, in which the elements on the borders will be equal to 1, and inside 0.
Click me to see the sample solution
29. Diagonal 5x5 Matrix (1-5)
Write a NumPy program to create a 5x5 zero matrix with elements on the main diagonal equal to 1, 2, 3, 4, 5.
Click me to see the sample solution
30. Staggered 4x4 Matrix (0,1)
Write a NumPy program to create a 4x4 matrix in which 0 and 1 are staggered, with zeros on the main diagonal.
Click me to see the sample solution
31. Create 3x3x3 Array of Values
Write a NumPy program to create a 3x3x3 array filled with arbitrary values.
Click me to see the sample solution
32. Compute Sums (All, Row, Column)
Write a NumPy program to compute the sum of all elements, the sum of each column and the sum of each row in a given array.
Click me to see the sample solution
33. Compute Inner Product of Vectors
Write a NumPy program to compute the inner product of two given vectors.
Click me to see the sample solution
34. Add Vector to Matrix Rows
Write a NumPy program to add a vector to each row of a given matrix.
Click me to see the sample solution
35. Save Array to Binary File
Write a NumPy program to save a given array to a binary file .
Click me to see the sample solution
36. Save Array and Load from Binary File
Write a NumPy program to save a given array to a binary file.
Click me to see the sample solution
37. Save and Load Array from Text File
Write a NumPy program to save a given array to a text file and load it.
Click me to see the sample solution
38. Convert Array to Bytes and Back
Write a NumPy program to convert a given array into bytes, and load it as an array.
Click me to see the sample solution
39. List to Array to List Comparison
Write a NumPy program to convert a given list into an array, then again convert it into a list. Check initial list and final list are equal or not.
Click me to see the sample solution
40. Sine Curve Coordinates with Plot
Write a NumPy program to compute the x and y coordinates for points on a sine curve and plot the points using matplotlib.
Click me to see the sample solution
41. Convert NumPy Dtypes to Python Types
Write a NumPy program to convert numpy dtypes to native Python types
Click me to see the sample solution
42. Add Elements Conditionally
Write a NumPy program to add elements to a matrix. If an element in the matrix is 0, we will not add the element below this element.
Click me to see the sample solution
43. Find Missing Data in Array
Write a NumPy program to find missing data in a given array.
Click me to see the sample solution
44. Compare Two Arrays (Element-Wise)
Write a NumPy program to check whether two arrays are equal (element wise) or not.
Click me to see the sample solution
45. Create 1D Array of Digits
Write a NumPy program to create a one-dimensional array of single, two and three-digit numbers.
Click me to see the sample solution
46. Create 2D Array of Specified Format
Write a NumPy program to create a two-dimensional array of a specified format.
Click me to see the sample solution
47. Create Array of Random Uniform [0,1]
Write a NumPy program to create a one-dimensional array of forty pseudo-randomly generated values. Select random numbers from a uniform distribution between 0 and 1.
Click me to see the sample solution
48. Random Normal Distribution 2D Array
Write a NumPy program to create a two-dimensional array with shape (8,5) of random numbers. Select random numbers from a normal distribution (200,7).
Click me to see the sample solution
49. Random Sampling with/without Replacement
Write a NumPy program to generate a uniform, non-uniform random sample from a given 1-D array with and without replacement.
Click me to see the sample solution
50. Swap Rows in 4x4 Random Array
Write a NumPy program to create a 4x4 array with random values. Create an array from the said array swapping first and last rows.
Click me to see the sample solution
51. Create Zero-Filled Array (5x6)
Write a NumPy program to create a new array of given shape (5,6) and type, filled with zeros.
Click me to see the sample solution
52. Sort Array by Rows & Columns
Write a NumPy program to sort a given array by row and column in ascending order.
Click me to see the sample solution
53. Extract Numbers Less/Greater Than Value
Write a NumPy program to extract all numbers from a given array less and greater than a specified number.
Click me to see the sample solution
54. Replace Numbers Based on Condition
Write a NumPy program to replace all numbers in a given array equal, less and greater than a given number.
Click me to see the sample solution
55. Create Array of Same Shape & Type
Write a NumPy program to create an array of equal shape and data type for a given array.
Click me to see the sample solution
56. Create 3D Array of Shape (3,5,4)
Write a NumPy program to create a three-dimensional array with the shape (3,5,4) and set it to a variable.
Click me to see the sample solution
57. Swap Columns in 4x4 Array
Write a NumPy program to create a 4x4 array. Create an array from said array by swapping first and last, second and third columns.
Click me to see the sample solution
58. Reverse Rows & Columns in Array
Write a NumPy program to swap rows and columns of a given array in reverse order.
Click me to see the sample solution
59. Element-Wise Multiply Two Arrays
Write a NumPy program to multiply two given arrays of the same size element-by-element.
Click me to see the sample solution
Python-Numpy Code Editor:
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics