NumPy provides many built-in mathematical functions that make calculations fast and efficient. These functions are widely used in data analysis, machine learning, scientific computing, and engineering. In this chapter, you’ll practice beginner-friendly NumPy mathematical function questions with complete solutions. NumPy Mathematical Functions practice questions with solutions help to understand the concepts.
1. Python Program to Find the Square Root of Numbers
Problem Statement
Write a Python program to find the square root of every element in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([4, 9, 16, 25, 36])
result = np.sqrt(numbers)
print(result)
Sample Output
[2. 3. 4. 5. 6.]
Explanation
The np.sqrt() function returns the square root of every element in the array.
Concepts Covered
sqrt()- Square Root
2. Python Program to Find the Square of Numbers
Problem Statement
Write a Python program to calculate the square of every element in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([2, 3, 4, 5])
result = np.square(numbers)
print(result)
Sample Output
[ 4 9 16 25]
Explanation
The np.square() function returns the square of each array element.
Concepts Covered
square()- Power Calculation
3. Python Program to Find the Cube of Numbers
Problem Statement
Write a Python program to calculate the cube of every element in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([2, 3, 4])
result = np.power(numbers, 3)
print(result)
Sample Output
[ 8 27 64]
Explanation
The np.power() function raises every element to the specified power.
Concepts Covered
power()- Cube
4. Python Program to Find Absolute Values
Problem Statement
Write a Python program to convert all negative numbers into positive numbers.
Python Solution
import numpy as np
numbers = np.array([-10, 20, -30, 40, -50])
result = np.absolute(numbers)
print(result)
Sample Output
[10 20 30 40 50]
Explanation
The np.absolute() function returns the absolute value of each element.
Concepts Covered
absolute()- Absolute Value
5. Python Program to Find the Sine of Angles
Problem Statement
Write a Python program to calculate the sine values of given angles.
Python Solution
import numpy as np
angles = np.array([0, np.pi/2, np.pi])
result = np.sin(angles)
print(result)
Sample Output
[0.0000000e+00 1.0000000e+00 1.2246468e-16]
Explanation
The np.sin() function calculates the sine of each angle in radians.
Concepts Covered
sin()- Trigonometry
6. Python Program to Find the Cosine of Angles
Problem Statement
Write a Python program to calculate cosine values of given angles.
Python Solution
import numpy as np
angles = np.array([0, np.pi/2, np.pi])
result = np.cos(angles)
print(result)
Sample Output
[ 1.000000e+00 6.123234e-17 -1.000000e+00]
Explanation
The np.cos() function calculates cosine values for every angle.
Concepts Covered
cos()- Trigonometric Functions
7. Python Program to Calculate Logarithm
Problem Statement
Write a Python program to calculate the natural logarithm of array elements.
Python Solution
import numpy as np
numbers = np.array([1, 2, 4, 8])
result = np.log(numbers)
print(result)
Sample Output
[0. 0.69314718 1.38629436 2.07944154]
Explanation
The np.log() function calculates the natural logarithm of each element.
Concepts Covered
log()- Natural Logarithm
8. Python Program to Find Exponential Values
Problem Statement
Write a Python program to calculate exponential values using NumPy.
Python Solution
import numpy as np
numbers = np.array([1, 2, 3])
result = np.exp(numbers)
print(result)
Sample Output
[ 2.71828183 7.3890561 20.08553692]
Explanation
The np.exp() function calculates e raised to the power of each element.
Concepts Covered
exp()- Exponential Function
9. Python Program to Find Maximum and Minimum Values
Problem Statement
Write a Python program to find the largest and smallest values in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([45, 12, 89, 30, 56])
print("Maximum:", np.max(numbers))
print("Minimum:", np.min(numbers))
Sample Output
Maximum: 89
Minimum: 12
Explanation
The np.max() and np.min() functions return the largest and smallest values.
Concepts Covered
max()min()
10. Python Program to Calculate Mean of an Array
Problem Statement
Write a Python program to calculate the average of all elements in 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 calculates the arithmetic mean of all array elements.
Concepts Covered
mean()- Average
Chapter Summary
In this chapter, you learned how to perform mathematical operations using NumPy. You practiced square roots, powers, absolute values, trigonometric functions, logarithms, exponential functions, maximum and minimum values, and averages. These functions are widely used in data analysis, machine learning, and scientific computing.
Key Takeaways
np.sqrt()calculates square roots.np.square()andnp.power()perform power calculations.np.absolute()converts negative values into positive values.np.sin()andnp.cos()calculate trigonometric values.np.log()computes natural logarithms.np.exp()calculates exponential values.np.max(),np.min(), andnp.mean()are essential statistical functions.
Frequently Asked Questions (FAQs)
1. What are NumPy mathematical functions?
NumPy mathematical functions are built-in functions used to perform mathematical operations efficiently on arrays.
2. Which function calculates square roots?
Use np.sqrt() to calculate the square root of array elements.
3. How do I calculate powers in NumPy?
Use np.square() for squares or np.power() for custom powers.
4. Which function returns absolute values?
Use np.absolute() to convert negative numbers into positive numbers.
5. How do I calculate the average of an array?
Use the np.mean() function.
6. Which functions return the maximum and minimum values?
Use np.max() for the largest value and np.min() for the smallest value.
7. Why are NumPy mathematical functions important?
They simplify complex calculations and are widely used in data science, machine learning, engineering, finance, and scientific research.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

