Python Practice › NumPy Programs
Python NumPy Programs with Solutions
100 NumPy programs: creating arrays and matrices, element-wise tests and comparisons, indexing, reshaping, sorting, set operations, NaN handling, statistics and rounding. The outputs come from NumPy 2.5; the in-browser Try it uses an older NumPy, so a few printouts may look slightly different.
Before you start
Read the NumPy lesson first if the topic is new to you.
All programs
- #1Get the NumPy version
Write a Python program to get the installed NumPy version.Easy - #2Test that no element is zero
Write a NumPy program to test whether none of the elements of an array is zero.Easy - #3Element-wise finite test
Write a NumPy program to test element-wise whether values are finite.Easy - #4Element-wise NaN test
Write a NumPy program to test element-wise for NaN.Easy - #5Element-wise complex test
Write a NumPy program to test element-wise for complex numbers.Easy - #6Element-wise real test
Write a NumPy program to test element-wise for real numbers.Easy - #7Is a value a scalar?
Write a NumPy program to test whether a value is a scalar.Easy - #8Are two arrays element-wise equal?
Write a NumPy program to test whether two arrays are element-wise equal.Easy - #9Element-wise greater than
Write a NumPy program for an element-wise “greater than” comparison.Easy - #10Element-wise greater or equal
Write a NumPy program for an element-wise “greater or equal” comparison.Easy - #11Element-wise less than
Write a NumPy program for an element-wise “less than” comparison.Easy - #12Create an array from values
Write a NumPy program to create an array with the values 10, 34, 86, 26, 56.Easy - #13Memory used by an array
Write a NumPy program to find the memory occupied by an array.Easy - #14Array of three ones
Write a NumPy program to create an array of 3 ones.Easy - #15Integers from 1 to 20
Write a NumPy program to create an array of integers from 1 to 20.Easy - #16Even integers from 1 to 20
Write a NumPy program to create an array of even integers from 1 to 20.Easy - #17Odd integers from 1 to 20
Write a NumPy program to create an array of odd integers from 1 to 20.Easy - #18Integers 1 to 100 in steps of 6
Write a NumPy program to create an array of integers from 1 to 100 with a difference of 6.Easy - #193 × 3 matrix
Write a NumPy program to create a 3 × 3 matrix.Easy - #203 × 3 identity matrix
Write a NumPy program to create a 3 × 3 identity matrix.Easy - #21Random numbers between 1 and 2
Write a NumPy program to generate random numbers between 1 and 2.Easy - #22Five standard normal numbers
Write a NumPy program to generate 5 random numbers from a standard normal distribution.Easy - #23Vector from 1 to 20
Write a NumPy program to create a vector with values 1 to 20.Easy - #24All values except 20
Write a NumPy program to print all values of the vector 1-20 except 20.Easy - #25All values except 1
Write a NumPy program to print all values of the vector 1-20 except 1.Easy - #263 × 4 matrix
Write a NumPy program to create a 3 × 4 matrix.Easy - #272 × 3 matrix
Write a NumPy program to create a 2 × 3 matrix.Easy - #282 × 2 matrix
Write a NumPy program to create a 2 × 2 matrix.Easy - #29Change the sign of a range
Write a NumPy program to create a vector of 1 to 15 and change the sign of the numbers from 6 to 10.Easy - #30Multiply two vectors
Write a NumPy program to multiply two vectors element-wise.Easy - #312 × 3 matrix from 5 to 10
Write a NumPy program to create a 2 × 3 matrix with values from 5 to 10.Easy - #32Shape of a matrix
Write a NumPy program to find the shape of a matrix.Easy - #33Reshape 2 × 3 into 3 × 2
Write a NumPy program to reshape a 2 × 3 matrix into a 3 × 2 matrix.Easy - #34Diagonal matrix
Write a NumPy program to create a 2 × 2 zero matrix with 7 and 8 on the main diagonal.Easy - #35Sum of all elements
Write a NumPy program to calculate the sum of all elements.Easy - #36Sum of each row
Write a NumPy program to calculate the sum of each row.Easy - #37Sum of each column
Write a NumPy program to calculate the sum of each column.Easy - #38Dot product of two vectors
Write a NumPy program to calculate the dot product of two vectors.Easy - #39Add a vector to each row
Write a NumPy program to add a vector to each row of a matrix.Easy - #40List to array
Write a NumPy program to convert a list into an array.Easy - #41Array to list
Write a NumPy program to convert an array into a list.Easy - #42Find missing data
Write a NumPy program to find the missing (NaN) values in an array.Easy - #43Element-wise equality
Write a NumPy program to check whether two arrays are equal element-wise.Easy - #44One-dimensional array
Write a NumPy program to create a one-dimensional array.Easy - #45Transpose a matrix
Write a NumPy program to swap the rows and columns of a matrix.Easy - #46Sort each row
Write a NumPy program to sort each row in ascending order.Easy - #47Sort rows by the first column
Write a NumPy program to sort the rows of an array by the first column.Easy - #48Numbers greater than a value
Write a NumPy program to extract numbers greater than 6.Easy - #49Replace numbers at or above a value
Write a NumPy program to replace numbers greater than or equal to 8 with 0.Easy - #50Reverse the row order
Write a NumPy program to swap the rows of an array (first and last).Easy - #51Multiply a row by a scalar
Write a NumPy program to multiply row 1 of an array by 2.Easy - #52Add one row to another
Write a NumPy program to add row 2 to row 1.Easy - #53Real and imaginary parts
Write a NumPy program to find the real and imaginary parts of complex numbers.Easy - #54Common elements of two arrays
Write a NumPy program to find the common elements of two arrays.Easy - #55Unique elements
Write a NumPy program to get the unique elements of an array.Easy - #56Set difference
Write a NumPy program to find the unique values of array1 that are not in array2.Easy - #57Union of two arrays
Write a NumPy program to find the union of two arrays.Easy - #58Index of the maximum
Write a NumPy program to find the index of the maximum value.Easy - #59Index of the minimum
Write a NumPy program to find the index of the minimum value.Easy - #60Change the data type
Write a NumPy program to change the data type of an array.Easy - #61The ith element of a 2-D array
Write a NumPy program to find the ith element (counting row by row) of a 2-D array.Easy - #62Remove single-dimensional entries
Write a NumPy program to remove dimensions of size 1 from a shape.Easy - #631-D arrays as columns
Write a NumPy program to combine two 1-D arrays as columns of a 2-D array.Easy - #64Split into 2, 3 and 5 elements
Write a NumPy program to split 10 elements into arrays of 2, 3 and 5 elements.Easy - #65Count non-zero elements
Write a NumPy program to count the non-zero elements.Easy - #66Median of a matrix
Write a NumPy program to find the median of all values in a matrix.Easy - #67Median of each row
Write a NumPy program to find the median of each row.Easy - #68Median of each column
Write a NumPy program to find the median of each column.Easy - #69Make an array read-only
Write a NumPy program to make an array immutable.Easy - #70Print every element
Write a NumPy program to print every element of a 2-D array.Easy - #71Square every element
Write a NumPy program to print the squares of all elements.Easy - #72Access by column
Write a NumPy program to print each column of an array.Easy - #73Float array to integers
Write a NumPy program to convert an array of floats to integers.Easy - #74Add a column
Write a NumPy program to add an extra column to an array.Easy - #75Replace negative values
Write a NumPy program to replace negative values with 1.Easy - #76Drop rows with NaN
Write a NumPy program to remove rows that contain NaN.Easy - #77Magnitude of a vector
Write a NumPy program to get the magnitude (length) of a vector.Easy - #78Frequency of unique values
Write a NumPy program to count how often each unique value appears.Easy - #79Is an array empty?
Write a NumPy program to check whether an array is empty.Easy - #80Product of an array
Write a NumPy program to calculate the product of an array.Easy - #81Save an array to CSV
Write a NumPy program to save an array to a CSV file.Easy - #82First two columns
Write a NumPy program to access the first two columns of an array.Easy - #83Top-left 2 × 2 block
Write a NumPy program to extract the first and second elements of the first and second rows.Easy - #84Copy an array
Write a NumPy program to copy data from one array to another.Easy - #85Sum of all columns
Write a NumPy program to calculate the sum of each column.Easy - #86Row averages ignoring NaN
Write a NumPy program to calculate the average of each row, ignoring NaNs.Easy - #87Merge arrays of the same shape
Write a NumPy program to merge two arrays of identical shape.Easy - #88Array to a DataFrame with headers
Write a NumPy program to convert an array into a pandas DataFrame with column headers.Easy - #89Create and reshape
Write a NumPy program to create an array and reshape it.Easy - #90Do two arrays have the same shape?
Write a NumPy program to check whether two arrays have the same dimensions.Easy - #91Sine of angles
Write a NumPy program to calculate the sine of angles.Easy - #92Standard deviation
Write a NumPy program to calculate the standard deviation of an array.Easy - #93Variance
Write a NumPy program to calculate the variance of an array.Easy - #94Cosine of angles
Write a NumPy program to calculate the cosine of angles.Easy - #95Tangent of angles
Write a NumPy program to calculate the tangent of angles.Easy - #96Round to two decimals
Write a NumPy program to round elements to two decimal places.Easy - #97Floor of each element
Write a NumPy program to round elements down with floor.Easy - #98Ceiling of each element
Write a NumPy program to round elements up with ceil.Easy - #99Tuple to array
Write a NumPy program to convert a tuple into an array.Easy - #100Complex dtype array
Write a NumPy program to create an array with complex dtype.Easy