How to calculate square root in Python

In this Python tutorial, we will learn how to calculate a square root in python by doing different examples using different methods.

There are multiple ways to find the square root of the given number in Python, which are shown below:

  • Using the math module
  • Using the exponent operator
  • Using the pow function
  • Using the cmath module
  • Using the numpy module
  • Using the decimal module
  • Using the fractions module
  • Using isqrt() function

Square Root in Python

Python provides several built-in methods and libraries for calculating square roots, making it easy to perform calculations involving square roots in Python.

In Python, there are various ways to calculate the square root of a number, including using the math module, the exponent operator **, the pow() function, etc.

Method-1: Calculate square root in Python using the math module

The math module in Python provides a range of mathematical functions, including the sqrt() function, which can be used to calculate the square root of a number.

# import the math module
import math

# Assign the value of 16 to x
x=16

# Calculate the square root of x using math.sqrt()
y=math.sqrt(x)

#print the square of x
print(y)

The above code imports the math module and assigns the value of 16 to a variable x.

  • It then calculates the square root of x using math.sqrt() function and assigns the result to a variable y.
  • Finally, it prints the value of y which is the square root of x.
Output: 4.0

Method-2: Calculate square root in Python using the exponent operator

Python provides an exponent operator ** that can be used to raise a number to a power. By raising a number to the power of 0.5, we can effectively calculate its square root.

# Assign the value 16 to the variable x
x = 16

# Compute the square root of x using the exponent operator and assign it to variable y
y = x ** 0.5

# Print the value of y, which is the square root of x
print(y)

The above code first assigns the value 16 to a variable x.

  • Next, the code calculates the square root of x using the exponent operator (**) and assigns the result to a variable y. The exponent operator raises the value of x to the power of 0.5, which is equivalent to taking the square root of x.
  • Finally, the code prints the value of y using the print() function.
Output: 4.0

Read: Python Addition Examples

Method-3: Calculate square root in Python using the pow function

The pow() function is similar to the exponent operator but takes two arguments: the base and the exponent. We can use the pow() function to calculate the square root of a number by passing it as the first argument and 0.5 as the second argument.

# Assign the value 16 to the variable x
x = 16

# Compute the square root of x using the pow() function and assign it to variable y
y = pow(x, 0.5)

# Print the value of y, which is the square root of x
print(y)

The above code first assigns the value 16 to a variable x.

  • Next, the code calculates the square root of x using the pow() function and assigns the result to a variable y. The pow() function takes two arguments: the first is the base (in this case, x), and the second is the exponent (in this case, 0.5).
  • The pow() function computes the value of the base raised to the power of the exponent.
  • Finally, the code prints the value of y using the print() function.
Output: 4.0

Method-4: Calculate square root in Python using the cmath module

The cmath module in Python provides functions for working with complex numbers. The sqrt() function in the cmath module can be used to calculate the square root of a negative number, returning a complex number.

# Import the cmath module, which provides support for complex numbers
import cmath

# Assign the value 64 to the variable x
x = 64

# Compute the square root of x using the sqrt() function from the cmath module and assign it to variable y
y = cmath.sqrt(x)

# Print the value of y, which is the square root of x
print(y)

The above code first imports the cmath module, which provides support for complex numbers in Python.

  • Next, the code assigns the value 64 to a variable x.
  • The code then uses the sqrt() function from the cmath module to compute the square root of x and assigns the result to a variable y. Since x is a positive real number, y will be a complex number with a real part equal to the square root of x and an imaginary part equal to 0.
  • Finally, the code prints the value of y using the print() function.
Output: (8+0j)

Read: Python Comparison Operators

Method-5: Calculate square root in Python using the numpy module

The numpy module is a popular library for scientific computing in Python. It provides many mathematical functions, including the sqrt() function, which can be used to calculate the square root of a number.

# Import the numpy module and alias it as np
import numpy as np

# Assign the value 16 to the variable x
x = 16

# Compute the square root of x using the sqrt() function from the numpy module
y = np.sqrt(x)

# Print the value of y, which is the square root of x
print(y)

The above code first imports the numpy module and aliases it as np.

  • Next, the code assigns the value 16 to a variable x.
  • The code then uses the sqrt() function from the numpy module to compute the square root of x and assigns the result to a variable y. Since x is a positive real number, y will be a real number with a value equal to the square root of x.
  • Finally, the code prints the value of y using the print() function.
Output: 4.0

Method-6: Calculate square root in Python using the decimal module

The decimal module in Python provides support for decimal floating-point arithmetic with arbitrary precision. We can use the sqrt() method from the Decimal class to calculate the square root of a number with specified precision.

# Import the Decimal and getcontext classes from the decimal module
from decimal import Decimal, getcontext

# Set the decimal precision to 50 digits
getcontext().prec = 50

# Create a Decimal object with the value 16 and assign it to the variable x
x = Decimal(16)

# Compute the square root of x using the sqrt() method from the Decimal class and assign it to the variable y
y = x.sqrt()

# Print the value of y, which is the square root of x as a Decimal object with 50 digits of precision
print(y)

The above code imports the Decimal and getcontext classes from the decimal module and sets the decimal precision to 50 digits using the getcontext().prec method.

  • It then creates a Decimal object with the value 16 and assigns it to the variable x.
  • The square root of x is computed using the sqrt() method from the Decimal class and assigned to the variable y.
  • Finally, the value of y is printed, which represents the square root of x as a Decimal object with 50 digits of precision.
Output: 4

Method-7: Calculate square root in Python using the fractions module

The fractions module in Python provides support for rational numbers. We can use the ** operator with a fractional exponent to calculate the square root of a number as a fraction.

# Import the Fraction module from the fractions library
from fractions import Fraction

# Create a Fraction object with value 16 and assign it to the variable x
x = Fraction(16)

# Calculate the square root of x using a Fraction object with value 1/2, and assign it to variable y
y = x ** Fraction(1, 2)

# Print the value of y
print(y)

The above code imports the Fraction class from the fractions module.

  • It creates a Fraction object with a value of 16 and assigns it to the variable x. It then calculates the square root of x by raising x to the power of a Fraction object with a value of 1/2, and assigns the result to the variable y.
  • Finally, it prints the value of y, which is the square root of x.
Output: 4.0

Method-8: Calculate square root in Python using isqrt() function

The isqrt() function is a lesser-known method for calculating the integer square root of a positive integer in Python.

# Import the math module
import math

# Assign the integer value 64 to variable x
x = 64

# Use the isqrt() function from the math module to calculate the integer square root of x
y = math.isqrt(x)

# Print the value of y
print(y)

The above code imports the math module, which provides mathematical functions and constants. It then assigns the integer value 64 to the variable x.

  • The code uses the isqrt() function from the math module to calculate the integer square root of x, and assigns the result to the variable y. The integer square root of a non-negative integer n is the largest integer m such that m*m <= n.
  • Finally, the code prints the value of y, which is the integer square root of x.
Output: 8

Conclusion

In this Python tutorial, we learned about 8 different methods for computing the square root in Python.

  • Using the math module
  • Using the exponent operator
  • Using the pow function
  • Using the cmath module
  • Using the numpy module
  • Using the decimal module
  • using the fractions module
  • Using isqrt() function

You may also like to read: