0
0
NumPydata~5 mins

np.sqrt() for square roots in NumPy

Choose your learning style9 modes available
Introduction

We use np.sqrt() to find the square root of numbers or each number in a list. It helps us quickly get the root values without doing the math by hand.

When you want to find the side length of a square given its area.
When calculating the standard deviation in statistics.
When working with distances in geometry or physics.
When normalizing data by scaling values using their roots.
When solving equations that require square root calculations.
Syntax
NumPy
np.sqrt(x)

x can be a single number or a list/array of numbers.

The function returns the square root of each element in x.

Examples
Find the square root of a single number 16.
NumPy
import numpy as np

result = np.sqrt(16)
print(result)
Find the square root of each number in an array.
NumPy
import numpy as np

arr = np.array([1, 4, 9, 16])
result = np.sqrt(arr)
print(result)
Square root of zero is zero.
NumPy
import numpy as np

result = np.sqrt(0)
print(result)
Sample Program

This program calculates the square root of each number in the list and prints both the original numbers and their square roots.

NumPy
import numpy as np

# Calculate square roots of a list of numbers
numbers = np.array([25, 36, 49, 64, 81])
square_roots = np.sqrt(numbers)

print("Original numbers:", numbers)
print("Square roots:", square_roots)
OutputSuccess
Important Notes

If you pass a negative number, np.sqrt() will return nan (not a number) because square root of negative numbers is not defined in real numbers.

You can use np.sqrt() on numpy arrays for fast calculations on many numbers at once.

Summary

np.sqrt() finds the square root of numbers or arrays.

It works on single numbers and lists/arrays of numbers.

Useful in many real-life math and data science problems.