sqrt Python | Find the Square Root in Python

The math.sqrt() method in Python is a built-in function of the math module, which is used to find the square root of a given number. This method takes a single argument, which is the number whose square root is to be calculated. It returns the square root of the input number as a floating-point value.


Syntax:
import math result = math.sqrt(x)
  1. x is the number whose square root is to be calculated
  2. result is the variable that stores the computed square root

Find the square root of a real number:

import math x = 25 result = math.sqrt(x) print(result) # Output: 5.0

Find the square root of a negative real number:

import math x = -25 result = math.sqrt(x) print(result) # Output: ValueError: math domain error

Here, you get a ValueError because the square root of a negative number is not a real number.

Find Square root in Python using the cmath module

The cmath module in Python provides functions for complex number math, including computing the square root of complex numbers. The cmath.sqrt() function can be used to compute the square root of a complex number.

Following is an example of how to use cmath.sqrt() to calculate the square root of a complex number:

import cmath z = 2 + 3j result = cmath.sqrt(z) print(result) # Output: (1.6741492280355401+0.8959774761298381j)

In the above example, first import the cmath module. Then define a complex number z with the value 2 + 3j. Use the cmath.sqrt() function to calculate the square root of z and store the result in the variable result. Finally, print the result, which is (1.6741492280355401+0.8959774761298381j).

Note that the result of cmath.sqrt() is a complex number, even if the input is a real number. If you want to convert the result back to a real number, you can use the abs() function to compute the magnitude of the complex number.

import cmath x = 25 result = cmath.sqrt(x) print(abs(result)) # Output: 5.0

In the above example, compute the square root of the real number 25 using cmath.sqrt(), which returns a complex number. Then use the abs() function to compute the magnitude of the complex number, which is the same as the absolute value of the real part. The result is 5.0, which is the square root of 25.

Square root of a negative real number

Finding the square root of a negative real number using complex numbers:

import cmath x = -25 result = cmath.sqrt(x) print(result) # Output: 5j

Here, use the c math.sqrt() method to find the square root of a negative number using complex numbers. The result is a complex number with an imaginary part of 5j.

Find Square root in Python using pow() function

In Python, you can use the pow() function to calculate the square root of a number by raising it to the power of 0.5.

x = 25 result = pow(x, 0.5) print(result) # Output: 5.0

In this example, calculate the square root of the number 25 by raising it to the power of 0.5 using the pow() function. The result is 5.0, which is the square root of 25.

Note that this method only works for real numbers. If you want to calculate the square root of a negative number or a complex number, you should use the cmath.sqrt() function from the cmath module instead.

Find the square root in using the exponent operator

In Python, you can use the exponent operator (**) to calculate the square root of a number by raising it to the power of 0.5, just like with the pow() function.

x = 25 result = x ** 0.5 print(result) # Output: 5.0

In this example, calculate the square root of the number 25 by raising it to the power of 0.5 using the exponent operator (**). The result is 5.0, which is the square root of 25.

The Python Square Root Function

The math.sqrt() method can be used in a variety of applications, including:

Find the length of the hypotenuse of a right-angled triangle:


How to calculate Square Root in Python?

In a right-angled triangle, the length of the hypotenuse (the longest side) can be calculated using the Pythagorean theorem. The theorem states that the square of the hypotenuse is equal to the sum of the squares of the other two sides. Using the math.sqrt() method, we can calculate the length of the hypotenuse as follows:

import math a = 3 b = 4 c = math.sqrt(a**2 + b**2) print(c) # Output: 5.0

Find the distance between two points:

The distance between two points in a two-dimensional plane can be calculated using the distance formula, which involves the square root of the sum of the squares of the differences between the coordinates. Using the math.sqrt() method, we can calculate the distance between two points as follows:

import math x1, y1 = 2, 3 x2, y2 = 5, 7 distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2) print(distance) # Output: 5.0

Calculating the standard deviation of a dataset:

The standard deviation is a measure of the amount of variation or dispersion in a dataset. It is calculated as the square root of the variance, which is the average of the squared differences from the mean. Using the math.sqrt() method, you can can calculate the standard deviation of a dataset as follows:

import math data = [2, 4, 6, 8, 10] mean = sum(data) / len(data) variance = sum((x - mean)**2 for x in data) / len(data) std_dev = math.sqrt(variance) print(std_dev) # Output: 2.8284271247461903

Calculating the magnitude of a vector:

The magnitude of a vector is the square root of the sum of the squares of its components. Using the math.sqrt() method, we can calculate the magnitude of a vector as follows:

import math v = [3, 4, 5] magnitude = math.sqrt(sum(x**2 for x in v)) print(magnitude) # Output: 7.0710678118654755