Python math.sqrt() – Square Root

Python math.sqrt()

math.sqrt(x) function returns the square root of x.

Syntax

The syntax to call sqrt() function is

math.sqrt(x)

where

ParameterRequiredDescription
xYesA numeric value.

If x < 0, then sqrt() raises ValueError.

Examples

Square root of 3.

Python Program

import math

x = 3
result = math.sqrt(x)
print('sqrt(x) :', result)
Run

Output

sqrt(x) : 1.7320508075688772

Square root of a negative number. sqrt() raises ValueError.

Python Program

import math

x = -3
result = math.sqrt(x)
print('sqrt(x) :', result)
Run

Output

ValueError: math domain error

Square root of infinity.

Python Program

import math

x = math.inf
result = math.sqrt(x)
print('sqrt(x) :', result)
Run

Output

sqrt(x) : inf

Square root of decimal value.

Python Program

import math

x = 0.001
result = math.sqrt(x)
print('sqrt(x) :', result)
Run

Output

sqrt(x) : 0.03162277660168379

Square root of nan.

Python Program

import math

x = math.nan
result = math.sqrt(x)
print('sqrt(x) :', result)
Run

Output

sqrt(x) : nan

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.sqrt() function.