How to Calculate square Root in Python

Published on Aug. 22, 2023, 12:16 p.m.

There are multiple ways to calculate the square root of a number in Python. Here are a few examples:

  1. Using the sqrt() function from the math module:
import math

num = 25
sqrt = math.sqrt(num)

print("The square root of", num, "is", sqrt)
  1. Using the exponential operator **:
num = 25
sqrt = num ** 0.5

print("The square root of", num, "is", sqrt)
  1. Using the sqrt() function from the NumPy module:
import numpy as np

num = 25
sqrt = np.sqrt(num)

print("The square root of", num, "is", sqrt)

All three methods will produce the same output, which is the square root of the input number:

The square root of 25 is 5.0

Note that the math and numpy modules support complex numbers as well as positive real numbers for the sqrt() function.

Tags: