NumPy Mathematical Functions Practice Questions with Solutions

Introduction

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

11. Python Program to Perform Basic Mathematical Operations on a NumPy Array

Problem Statement

Write a Python program to create a NumPy array and perform the following mathematical operations on each element:

  • Addition of 10
  • Subtraction of 5
  • Multiplication by 3
  • Division by 2

Python Solution

import numpy as np

array = np.array([10, 20, 30, 40, 50])

print("Original Array:")
print(array)

print("\nAfter Adding 10:")
print(array + 10)

print("\nAfter Subtracting 5:")
print(array - 5)

print("\nAfter Multiplying by 3:")
print(array * 3)

print("\nAfter Dividing by 2:")
print(array / 2)

Sample Output

Original Array:
[10 20 30 40 50]

After Adding 10:
[20 30 40 50 60]

After Subtracting 5:
[ 5 15 25 35 45]

After Multiplying by 3:
[ 30  60  90 120 150]

After Dividing by 2:
[ 5. 10. 15. 20. 25.]

Explanation

NumPy performs element-wise mathematical operations efficiently without using loops.

Concepts Covered

  • Element-wise Operations
  • Arithmetic Operators
  • Vectorized Computation

12. Python Program to Calculate Square, Square Root, and Cube of Array Elements

Problem Statement

Write a Python program to calculate the square, square root, and cube of every element in a NumPy array.

Python Solution

import numpy as np

array = np.array([4, 9, 16, 25, 36])

print("Original Array:")
print(array)

print("\nSquare:")
print(np.square(array))

print("\nSquare Root:")
print(np.sqrt(array))

print("\nCube:")
print(np.power(array, 3))

Sample Output

Original Array:
[ 4  9 16 25 36]

Square:
[  16   81  256  625 1296]

Square Root:
[2. 3. 4. 5. 6.]

Cube:
[   64   729  4096 15625 46656]

Explanation

NumPy provides built-in mathematical functions to perform power and root calculations on arrays.

Concepts Covered

  • np.square()
  • np.sqrt()
  • np.power()

13. Python Program to Calculate Trigonometric Functions

Problem Statement

Write a Python program to calculate the sine, cosine, and tangent values of given angles in degrees.

Python Solution

import numpy as np

angles = np.array([0, 30, 45, 60, 90])

radians = np.radians(angles)

print("Angles (Degrees):")
print(angles)

print("\nSine Values:")
print(np.sin(radians))

print("\nCosine Values:")
print(np.cos(radians))

print("\nTangent Values:")
print(np.tan(radians))

Sample Output

Angles (Degrees):
[ 0 30 45 60 90]

Sine Values:
[0.        0.5       0.70710678 0.8660254 1.       ]

Cosine Values:
[1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]

Tangent Values:
[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]

Explanation

NumPy trigonometric functions accept angles in radians, so degrees must first be converted using np.radians().

Concepts Covered

  • np.sin()
  • np.cos()
  • np.tan()
  • np.radians()

14. Python Program to Calculate Logarithmic and Exponential Values

Problem Statement

Write a Python program to calculate:

  • Natural logarithm
  • Base-10 logarithm
  • Exponential value

for every element of a NumPy array.

Python Solution

import numpy as np

array = np.array([1, 2, 5, 10])

print("Original Array:")
print(array)

print("\nNatural Log:")
print(np.log(array))

print("\nBase-10 Log:")
print(np.log10(array))

print("\nExponential:")
print(np.exp(array))

Sample Output

Natural Log:
[0.         0.69314718 1.60943791 2.30258509]

Base-10 Log:
[0.      0.30103 0.69897 1.     ]

Exponential:
[2.71828183e+00 7.38905610e+00 1.48413159e+02 2.20264658e+04]

Explanation

These mathematical functions are commonly used in machine learning, statistics, and scientific computing.

Concepts Covered

  • np.log()
  • np.log10()
  • np.exp()

15. Python Program to Calculate Mean, Median, and Standard Deviation

Problem Statement

Write a Python program to calculate the mean, median, variance, and standard deviation of a NumPy array.

Python Solution

import numpy as np

array = np.array([12, 18, 25, 30, 42, 55])

print("Array:")
print(array)

print("\nMean:", np.mean(array))
print("Median:", np.median(array))
print("Variance:", np.var(array))
print("Standard Deviation:", np.std(array))

Sample Output

Array:
[12 18 25 30 42 55]

Mean: 30.333333333333332
Median: 27.5
Variance: 204.88888888888889
Standard Deviation: 14.31394199369252

Explanation

These statistical functions help summarize the distribution of data in a NumPy array.

Concepts Covered

  • np.mean()
  • np.median()
  • np.var()
  • np.std()

16. Python Program to Calculate Cumulative Sum and Cumulative Product

Problem Statement

Write a Python program to calculate the cumulative sum and cumulative product of a NumPy array.

Python Solution

import numpy as np

array = np.array([2, 3, 4, 5])

print("Original Array:")
print(array)

print("\nCumulative Sum:")
print(np.cumsum(array))

print("\nCumulative Product:")
print(np.cumprod(array))

Sample Output

Original Array:
[2 3 4 5]

Cumulative Sum:
[ 2  5  9 14]

Cumulative Product:
[  2   6  24 120]

Explanation

  • np.cumsum() returns the running total.
  • np.cumprod() returns the running multiplication of elements.

Concepts Covered

  • np.cumsum()
  • np.cumprod()
  • Running Calculations

17. Python Program to Round Decimal Values Using Different NumPy Functions

Problem Statement

Write a Python program to round decimal numbers using:

  • round()
  • floor()
  • ceil()

Python Solution

import numpy as np

array = np.array([2.35, 4.78, 6.12, 8.99])

print("Original Array:")
print(array)

print("\nRounded Values:")
print(np.round(array))

print("\nFloor Values:")
print(np.floor(array))

print("\nCeiling Values:")
print(np.ceil(array))

Sample Output

Original Array:
[2.35 4.78 6.12 8.99]

Rounded Values:
[2. 5. 6. 9.]

Floor Values:
[2. 4. 6. 8.]

Ceiling Values:
[3. 5. 7. 9.]

Explanation

  • round() rounds to the nearest integer.
  • floor() rounds down.
  • ceil() rounds up.

Concepts Covered

  • np.round()
  • np.floor()
  • np.ceil()

18. Python Program to Calculate Absolute Difference Between Two Arrays

Problem Statement

Write a Python program to create two NumPy arrays and calculate the absolute difference between their corresponding elements.

Python Solution

import numpy as np

array1 = np.array([25, 40, 60, 80, 100])

array2 = np.array([20, 45, 55, 90, 95])

difference = np.abs(array1 - array2)

print("First Array:")
print(array1)

print("\nSecond Array:")
print(array2)

print("\nAbsolute Difference:")
print(difference)

Sample Output

First Array:
[ 25  40  60  80 100]

Second Array:
[20 45 55 90 95]

Absolute Difference:
[ 5  5  5 10  5]

Explanation

The np.abs() function returns the absolute value of each element after subtraction, ensuring all results are positive.

Concepts Covered

  • np.abs()
  • Array Arithmetic
  • Absolute Difference
  • Vectorized Operations

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() and np.power() perform power calculations.
  • np.absolute() converts negative values into positive values.
  • np.sin() and np.cos() calculate trigonometric values.
  • np.log() computes natural logarithms.
  • np.exp() calculates exponential values.
  • np.max(), np.min(), and np.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.

Scroll to Top