NumPy is a powerful Python library used for numerical computing and data analysis. It helps you work with arrays, perform mathematical operations, and process large datasets efficiently. In this practice set, you’ll solve beginner-friendly NumPy questions with complete solutions. Python NumPy practice Questions with Solutions help to understand the concepts.
1. Python Program to Create a NumPy Array
Difficulty: Foundation
Problem Statement
Write a Python program to create a NumPy array containing the numbers 10, 20, 30, 40, and 50.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50])
print(numbers)
Sample Output
[10 20 30 40 50]
Explanation
The np.array() function creates a NumPy array from a Python list.
Concepts Covered
- NumPy
- np.array()
2. Python Program to Find the Shape of an Array
Difficulty: Foundation
Problem Statement
Write a Python program to print the shape of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([[1, 2, 3], [4, 5, 6]])
print(numbers.shape)
Sample Output
(2, 3)
Explanation
The shape attribute returns the number of rows and columns in an array.
Concepts Covered
- shape
- 2D Array
3. Python Program to Access Array Elements
Difficulty: Foundation
Problem Statement
Write a Python program to print the third element of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([5, 10, 15, 20, 25])
print(numbers[2])
Sample Output
15
Explanation
Array indexing starts from 0, so index 2 returns the third element.
Concepts Covered
- Array Indexing
4. Python Program to Slice a NumPy Array
Difficulty: Foundation
Problem Statement
Write a Python program to print the first four elements of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50, 60])
print(numbers[:4])
Sample Output
[10 20 30 40]
Explanation
Array slicing extracts a portion of the array.
Concepts Covered
- Array Slicing
5. Python Program to Add Two NumPy Arrays
Difficulty: Practice
Problem Statement
Write a Python program to add two NumPy arrays.
Python Solution
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
result = array1 + array2
print(result)
Sample Output
[5 7 9]
Explanation
NumPy performs element-wise addition between arrays.
Concepts Covered
- Array Addition
6. Python Program to Multiply an Array by a Number
Difficulty: Practice
Problem Statement
Write a Python program to multiply every element of an array by 5.
Python Solution
import numpy as np
numbers = np.array([2, 4, 6, 8])
print(numbers * 5)
Sample Output
[10 20 30 40]
Explanation
NumPy supports vectorized mathematical operations.
Concepts Covered
- Array Multiplication
7. Python Program to Find the Maximum Value in an Array
Difficulty: Practice
Problem Statement
Write a Python program to find the largest value in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([15, 42, 8, 91, 30])
print(np.max(numbers))
Sample Output
91
Explanation
The np.max() function returns the largest value in an array.
Concepts Covered
- np.max()
8. Python Program to Reshape an Array
Difficulty: Challenge
Problem Statement
Write a Python program to convert a one-dimensional array into a 2 × 3 array.
Python Solution
import numpy as np
numbers = np.array([1, 2, 3, 4, 5, 6])
print(numbers.reshape(2, 3))
Sample Output
[[1 2 3]
[4 5 6]]
Explanation
The reshape() method changes the dimensions of an array without changing its data.
Concepts Covered
- reshape()
9. Python Program to Create an Array of Zeros
Difficulty: Challenge
Problem Statement
Write a Python program to create a 3 × 3 array filled with zeros.
Python Solution
import numpy as np
print(np.zeros((3, 3), dtype=int))
Sample Output
[[0 0 0]
[0 0 0]
[0 0 0]]
Explanation
The zeros() function creates an array filled with zeros.
Concepts Covered
- np.zeros()
10. Python Program to Calculate the Mean of an Array
Difficulty: Challenge
Problem Statement
Write a Python program to calculate the average value of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50])
print(np.mean(numbers))
Sample Output
30.0
Explanation
The np.mean() function returns the average of all array elements.
Concepts Covered
- np.mean()
Chapter Summary
After completing this chapter, you have learned:
- Creating NumPy arrays
- Array indexing
- Array slicing
- Array shape
- Array addition
- Array multiplication
- Finding maximum values
- Reshaping arrays
- Creating zero arrays
- Calculating mean values
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
