Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python cmath.sqrt() Function



The Pythoncmath.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 Pythoncmath.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 usingcmath.sqrt() function.

import cmathx=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 thecmath.sqrt() function.This gives 0j as the result.

import cmathnum = 0res = 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 usingcmath.sqrt() function.

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

Output

The result is obtained as follows −

2j6j9j

Example 4

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

import cmathprint(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
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp