How to Find the Square Root in Python (5 Ways)

To get a square root in Python, import the math module and call math.sqrt(x). It returns a float, so math.sqrt(144) gives 12.0.

Negative inputs, huge integers and arrays each need a different tool.

In this tutorial, I’ll show you five ways to find the square root in Python on one small set of patio orders. Every snippet ran on Python 3.12.5 and NumPy 2.5.3.

The sample patio areas we’ll use for square roots

A Phoenix landscaping crew sells square patios; each side is the square root of the area. Every example imports this file:

# Square patio orders for a Phoenix landscaping crew, 03/16/2026
areas = [144, 225, 310, 400, 0]   # square feet
typo = -16                        # a bad entry on one order form
EDGING_PER_FT = 4.75              # USD per linear foot of edging

Three areas are perfect squares, 310 isn’t, 0 is an edge case, and typo is the negative value. The capitals in EDGING_PER_FT mark a constant, per the Python naming conventions.

Method 1: Find the square root in Python with math.sqrt() (Recommended)

Use this for single numbers, which is most of what you’ll need. Here’s the areas list priced out:

import math
from patios import areas, EDGING_PER_FT

for area in areas:
    side = math.sqrt(area)
    edging = 4 * side * EDGING_PER_FT
    print(f"{area:>4} sq ft -> side {side:6.2f} ft, edging ${edging:,.2f}")

print(math.sqrt(144), type(math.sqrt(144)).__name__)

Output:

 144 sq ft -> side  12.00 ft, edging $228.00
 225 sq ft -> side  15.00 ft, edging $285.00
 310 sq ft -> side  17.61 ft, edging $334.53
 400 sq ft -> side  20.00 ft, edging $380.00
   0 sq ft -> side   0.00 ft, edging $0.00
12.0 float

The 144 sq ft patio has 12.00 ft sides and costs $228.00 to edge; 310 sq ft works out to 17.61 ft and $334.53.

Command Prompt output of Python math.sqrt() turning patio areas into side lengths and edging costs
Each area becomes a side length, and the last line shows the result is a float.

How does this code work?

math.sqrt(area) returns the side as a float, even for 144; times 4 and the rate gives dollars. The rest of the toolbox is in the Python math module guide.

The :6.2f spec rounds only the display, as explained in printing 2 decimal places in Python.

Method 2: Square root with the ** operator and pow()

Raising a number to the power 0.5 gives its square root, no import needed. Same patio data, now the 310 sq ft order:

import math
from patios import areas, typo

area = areas[2]                     # 310 sq ft
print("math.sqrt:", math.sqrt(area))
print("** 0.5:   ", area ** 0.5)
print("pow():    ", pow(area, 0.5))

print(-16 ** 0.5)                   # read as -(16 ** 0.5)
print(typo ** 0.5)                  # negative base -> complex number
print(64 ** (1 / 3), math.cbrt(64)) # cube root

Output:

math.sqrt: 17.60681686165901
** 0.5:    17.60681686165901
pow():     17.60681686165901
-4.0
(2.4492935982947064e-16+4j)
3.9999999999999996 4.0

All three return 17.60681686165901, but -16 ** 0.5 prints -4.0 and typo ** 0.5 prints a complex number.

Command Prompt output comparing math.sqrt, ** 0.5 and pow() in Python, plus a negative base and a cube root
The three methods agree on 310; the negative cases differ.

How does this code work?

** binds tighter than the minus sign, so -16 ** 0.5 means -(16 ** 0.5) (see operator precedence in Python). With a negative variable, you get a complex number, not an error.

The exponents in Python and Python pow() method guides cover other powers.

Important: 64 ** (1 / 3) gives 3.9999999999999996. math.cbrt(64) returns a clean 4.0 (Python 3.11+).

Method 3: Get an integer square root with math.isqrt()

For whole numbers, math.isqrt() (Python 3.8+) returns the floor of the root as an int. Same areas list:

import math
from patios import areas

for area in areas[:4]:
    root = math.isqrt(area)
    print(f"{area}: isqrt = {root}, perfect square: {root * root == area}")

n = 100_000_000_000_000_001         # an 18-digit integer
square = n * n
print("isqrt:     ", math.isqrt(square))
print("int(sqrt): ", int(math.sqrt(square)))

Output:

144: isqrt = 12, perfect square: True
225: isqrt = 15, perfect square: True
310: isqrt = 17, perfect square: False
400: isqrt = 20, perfect square: True
isqrt:      100000000000000001
int(sqrt):  100000000000000000

144, 225 and 400 pass; 310 gives 17 and fails. On the 18-digit number, isqrt() is exact and int(math.sqrt()) is off by one.

Command Prompt output of Python math.isqrt() checking perfect squares and staying exact on a large integer
isqrt() flags 310 as not a perfect square and keeps the last digit of a huge integer.

How does this code work?

isqrt(310) is 17 because 17 × 17 = 289 and 18 × 18 = 324. Squaring the root back is the perfect-square test.

math.sqrt() converts to a float (about 15-17 significant digits), so the last digit is lost. To test types first, see checking if a variable is an integer in Python.

Method 4: Square root of a list or array with NumPy

np.sqrt() handles a whole list in one call. Same patio list:

import numpy as np
from patios import areas, typo, EDGING_PER_FT

sides = np.sqrt(areas)
print(sides.round(2))
print(f"Total edging: ${(4 * sides * EDGING_PER_FT).sum():,.2f}")

print(np.sqrt([typo, 81]))          # nan plus a RuntimeWarning
print(np.emath.sqrt([typo, 81]))    # complex result instead of nan

Output:

[12.   15.   17.61 20.    0.  ]
Total edging: $1,227.53
C:\pyguides\patio_numpy.py:8: RuntimeWarning: invalid value encountered in sqrt
  print(np.sqrt([typo, 81]))          # nan plus a RuntimeWarning
[nan  9.]
[0.+4.j 9.+0.j]

All five sides come back at once; the edging totals $1,227.53.

Command Prompt output of NumPy np.sqrt() on the patio areas, a RuntimeWarning for a negative value and np.emath.sqrt
np.sqrt() returns nan for -16 with a RuntimeWarning; np.emath.sqrt() returns 4j instead.

How does this code work?

np.sqrt(areas) returns an array, so the cost math runs on every element. A negative value becomes nan plus a warning that’s easy to miss. Use np.emath.sqrt() for complex results, or fix signs with NumPy absolute value.

How do you take the square root of a negative number in Python?

This is where math.sqrt() stops. Let’s feed it the typo value:

import cmath
import math
from patios import typo

try:
    print(math.sqrt(typo))
except ValueError as err:
    print("math.sqrt(-16) ->", type(err).__name__ + ":", err)

root = cmath.sqrt(typo)
print("cmath.sqrt(-16) ->", root, "| imag part:", root.imag)
print("cmath.sqrt(144) ->", cmath.sqrt(144))
print("math.sqrt(abs(-16)) ->", math.sqrt(abs(typo)))

Output:

math.sqrt(-16) -> ValueError: math domain error
cmath.sqrt(-16) -> 4j | imag part: 4.0
cmath.sqrt(144) -> (12+0j)
math.sqrt(abs(-16)) -> 4.0

math.sqrt(-16) raises ValueError: math domain error, while cmath.sqrt(-16) returns 4j.

Command Prompt output of math.sqrt(-16) raising ValueError and cmath.sqrt(-16) returning 4j
math refuses the negative input; cmath returns an imaginary result.

How does this code work?

For patio data, abs() is the honest fix. math handles real numbers only, hence ValueError: math domain error. cmath returns complex numbers, even (12+0j) for 144.

Which square root method should you use in Python?

MethodUse it whenWatch out for
math.sqrt(x)One real number (default)ValueError on negatives
x ** 0.5 / pow()No import wantedComplex result on negatives
math.isqrt(n)Exact integers, perfect squaresInts only, returns the floor
np.sqrt(arr)Lists, arrays, columnsnan on negatives
cmath.sqrt(x)You want complex resultsAlways returns complex

I’d use math.sqrt() by default and switch to isqrt() when your integers must stay exact.

Things to keep in mind with square roots in Python

  • Strings fail. Convert input() or CSV values with float() first.
  • Floats round. Compare roots with math.isclose(), not ==.
  • Squaring is the reverse. See the square function in Python.

Want more number crunching? Try these next:

Finding a square root is also a root-finding problem: solve x² − n = 0. My bisection method in Python tutorial shows how to narrow down an answer like that by halving an interval.

Frequently asked questions

How do you find the square root in Python?

Call math.sqrt(x) after import math; math.sqrt(144) returns 12.0.

Can I take a square root in Python without importing math?

Yes. x ** 0.5 or pow(x, 0.5) give the same float for positive numbers.

Why does math.sqrt() return a float?

It works in floating point. Use math.isqrt() when you need an int.

How do I check if a number is a perfect square in Python?

Use r = math.isqrt(n) and check r * r == n.