Python cmath.sqrt() Function



The Python cmath.sqrt() function is used to find the square root of a complex number. The square root of a given number is the factor multiplying by itself to get that particular number.

Syntax

Following is the syntax of Python cmath.sqrt() function −

cmath.sqrt(x)

Parameters

This function accepts any number i.e greater than or equal to 0.

Return Value

This method returns the square root of a specified value.

Example 1

In the following example we are passing different positive values in the square root of the given number by using cmath.sqrt() function.

import cmath
x=cmath.sqrt(200)
y=cmath.sqrt(36)
z=cmath.sqrt(cmath.pi)
print(x,y,z)

Output

When we run the above program, it produces following result −

(14.142135623730951+0j) (6+0j) (1.7724538509055159+0j)

Example 2

Here we are passing zero as an argument to the cmath.sqrt() function.This gives 0j as the result.

import cmath
num = 0
res = cmath.sqrt(num)
print('The square root of zero is : ', res)

Output

The above program generates the following output −

The square root of zero is : 0j
python_modules.htm

Example 3

Now, we are calculating negative values in the square root of the given number using cmath.sqrt() function.

import cmath
print(cmath.sqrt(-4))
print(cmath.sqrt(-36))
print(cmath.sqrt(-81))

Output

The result is obtained as follows −

2j
6j
9j

Example 4

Here, we are calculating floating point values in the square root using cmath.sqrt() function.

import cmath
print(cmath.sqrt(0.4))
print(cmath.sqrt(3.6))
print(cmath.sqrt(0.81))

Output

This produces the following result −

(0.6324555320336759+0j)
(1.8973665961010275+0j)
(0.9+0j)
python_modules.htm
Advertisements