Python math.sqrt() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

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)
CallWhat 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

ParameterDescription
xRequired positional number (int or float) greater than or equal to 0. Complex numbers raise TypeError.

Return Value

Return ValueDescription
floatSquare 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))

How It Works

  1. Four sample areas. The loop passes perfect squares, a non-square integer, and a decimal area into math.sqrt().
  2. Fixed four-decimal display. Formatting with :.4f turns the irrational root of 2 into 1.4142.
  3. Always returns float. Checking type(...).__name__ confirms math.sqrt(25) yields float, while math.isqrt() yields integer 5.

Example 2 — Calculate Free-Fall Drop Times in Physics

Compute how many seconds an object takes to drop from three heights using t = sqrt(2h / g).

python
import math

gravity = 9.81
for height in [4.905, 19.62, 44.145]:
    seconds = math.sqrt(2.0 * height / gravity)
    print(f"height={height}m -> time={seconds:.2f}s")

How It Works

  1. Multiply before dividing. Twice each chosen height divided by 9.81 simplifies cleanly to 1.0, 4.0, and 9.0.
  2. Extract drop duration. Taking the square root gives exact drop durations of one, two, and three seconds.
  3. 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)))

How It Works

  1. Domain error caught. Passing -9 raises ValueError; use cmath.sqrt(-9) when complex answers like 3j are needed.
  2. Infinity propagates. Positive infinity has an infinite square root, printed as inf.
  3. NaN stays NaN. Checking with math.isnan() confirms a NaN input produces a NaN result.
  4. 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.

Next: math.sumprod()

Return the sum of products of values from two iterables (Python 3.12+).

sumprod() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful