math.sqrt(x) returns the non-negative square root of x as a float, even when x is a perfect integer square such as 25.
Real numbers below zero have no real square root, so math.sqrt(-1) raises ValueError. For complex square roots use the cmath module, and for exact integer floor roots of large integers use math.isqrt().
math.sqrt() is a function in Python's standard math module: import it with import math and call math.sqrt(...), or import the name directly with from math import sqrt.
Syntax
Import math and pass one non-negative real number.
python
import math
root = math.sqrt(x)
Call
What it does
math.sqrt(25)
Returns 5.0 (always a float).
math.sqrt(2)
Returns 1.4142135623730951.
math.sqrt(0)
Returns 0.0.
math.sqrt(-4)
Raises ValueError: negative real input.
For huge integers past 53 bits, math.isqrt() avoids float rounding; for cube roots use math.cbrt() (Python 3.11+).
Function signatures
python
math.sqrt(x, /)
# returns: float square root for x >= 0
Parameters
Parameter
Description
x
Required positional number (int or float) greater than or equal to 0. Complex numbers raise TypeError.
Return Value
Return Value
Description
float
Square root of x. Preserves -0.0, returns inf for inf, and nan for nan.
Python Examples
Example 1 — Find Side Lengths From Square Garden Areas
Convert square areas to side lengths and compare math.sqrt() with math.isqrt() on an integer.
python
import math
areas = [25, 144, 2, 0.25]
for area in areas:
side = math.sqrt(area)
print(f"area={area} -> side={side:.4f}")
print(type(math.sqrt(25)).__name__, math.isqrt(25))
Multiply before dividing. Twice each chosen height divided by 9.81 simplifies cleanly to 1.0, 4.0, and 9.0.
Extract drop duration. Taking the square root gives exact drop durations of one, two, and three seconds.
Readable seconds output. Formatting to two decimal places keeps the table aligned for quick inspection.
Example 3 — Handle Negative Inputs and Special IEEE Values
Catch ValueError on negative numbers and inspect infinity and NaN propagation.
python
import math
try:
math.sqrt(-9)
except ValueError:
print("negative input requires cmath for complex roots")
print(math.sqrt(float("inf")))
print(math.isnan(math.sqrt(float("nan"))))
print(math.copysign(1.0, math.sqrt(-0.0)))
Output
negative input requires cmath for complex roots
inf
True
-1.0
How It Works
Domain error caught. Passing -9 raises ValueError; use cmath.sqrt(-9) when complex answers like 3j are needed.
Infinity propagates. Positive infinity has an infinite square root, printed as inf.
NaN stays NaN. Checking with math.isnan() confirms a NaN input produces a NaN result.
Signed zero preserved. By IEEE 754 rules, math.sqrt(-0.0) keeps the negative sign bit, revealed via math.copysign().
Frequently Asked Questions
It still returns a float, such as 5.0 for math.sqrt(25). Use math.isqrt(25) when you want an int.
The math module handles only real numbers, and negative real numbers have no real square root. Import cmath and call cmath.sqrt(-1) for 1j.
They compute the same value for non-negative numbers, while math.sqrt(x) states your intent clearly and avoids returning complex numbers by surprise on negative inputs.
Call math.isqrt(n). Converting a huge integer to float for math.sqrt(n) loses precision past 53 bits or raises OverflowError.
No. Passing 4+0j to math.sqrt() raises TypeError. Use cmath.sqrt() instead.