🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI Free4 How to Type Square Root in Python

Writing a square-root calculation in Python begins with deciding what kind of answer the calculation needs. Python can return a fractional square root or an integer floor root.
This article explains how to choose among Python’s square-root methods and what to do with negative inputs.
TL;DR: Choose the right Python square root
Use math.sqrt(x) for one nonnegative number, and choose a different function when you need a complex answer, an array result or an integer floor.
- math.sqrt(x) is the standard choice for a nonnegative input.
- Use x ** 0.5 or pow(x, 0.5) for a fractional power.
- Use cmath.sqrt(x) for complex inputs, or numpy.sqrt(x) for element-wise array roots.
- Choose math.isqrt(n) for the integer floor root.
What is a square root in Python?
A square root is a value that, multiplied by itself, gives the input, so both 3 and -3 square to 9.
Square-root calculations appear in distance formulas and normalization, where a fractional result can feed a later calculation. The table distinguishes the returned types before you choose a function.
| Method | Use it for | Result |
|---|---|---|
| math.sqrt | One nonnegative number | Float |
| ** 0.5 or pow | Fractional power without a math import | Float or complex |
| cmath.sqrt | Negative or complex input | Complex |
| numpy.sqrt | Each value in an array | Scalar or array |
| math.isqrt | Integer floor root | Integer |
How to calculate a square root in Python step by step
I ran python3 sqrt_demo.py and got matching values from math.sqrt(2) and 2 ** 0.5: 1.4142135623730951.
Step 1: Calculate a standard square root
Import math and call math.sqrt() for a single nonnegative number. The returned value is a float, including when the input is an integer.
Step 2: Compare exponentiation and pow()
Raising a value to one-half computes its square root. The built-in pow() and the ** operator express this directly. math.pow() is separate and converts its arguments to floats.
Read the built-in pow() guide for its broader behavior. A fractional exponent returns an approximation, which can lose precision with very large integer inputs.
For a negative base, Python evaluates exponentiation before unary minus, which can change how an expression is read. Group the base when the negative sign belongs to the value.
Step 3: Use cmath for complex roots
No number on the number line squares to a negative value, but complex numbers provide a square root. cmath.sqrt(-9) returns 3j, where j denotes the imaginary unit. cmath returns a complex value even when the input is positive.
Step 4: Apply roots to arrays or integers
When the input is a NumPy array, numpy.sqrt() applies the operation element by element. For integer-only work, math.isqrt(10) returns 3 because 3 squared is no greater than 10 while 4 squared is greater than 10.
NumPy also accepts a scalar, but it is useful when a calculation already uses arrays. Its output follows the input shape, which lets the result feed into later array operations without a manual loop. For one isolated value, importing NumPy only for a square root adds a dependency without adding much.
For a negative entry in an array with a non-complex dtype, NumPy returns nan for that position and raises a runtime warning. Convert the array to a complex dtype or use numpy.emath.sqrt() when complex outputs are intended. That choice affects every element’s output type, not only the negative entry.
import cmath
import math
import numpy as np
value = 2
print("math.sqrt(2):", math.sqrt(value))
print("2 ** 0.5:", value ** 0.5)
print("math.isqrt(10):", math.isqrt(10))
print("cmath.sqrt(-9):", cmath.sqrt(-9))
print("numpy.sqrt([4, 9]):", np.sqrt([4, 9]))
print("math.pow(9, 0.5):", math.pow(9, 0.5))
try:
math.sqrt(-9)
except ValueError as error:
print(type(error).__name__ + ":", error)
Output:
math.sqrt(2): 1.4142135623730951
2 ** 0.5: 1.4142135623730951
math.isqrt(10): 3
cmath.sqrt(-9): 3j
numpy.sqrt([4, 9]): [2. 3.]
math.pow(9, 0.5): 3.0
ValueError: expected a nonnegative input, got -9.0

The first two values match for this input, while math.isqrt() intentionally drops the fractional part. Use the Python math module when you need other standard mathematical functions too.
Square-root edge cases and fixes
The executed example shows the negative-input failure first. Oversized integers and text require different responses, as the table shows.
| Input or need | What happens | Choice |
|---|---|---|
| Zero | Returns 0.0 from math.sqrt | Call math.sqrt(0) |
| Negative input with math.sqrt | Raises ValueError. I passed -9 in the script and got ValueError: expected a nonnegative input, got -9.0. | Use cmath.sqrt if a complex result is appropriate |
| Negative input to numpy.sqrt | Produces nan and a runtime warning for non-complex input | Use complex-valued input or numpy.emath.sqrt when appropriate |
| Very large integer or exact integer-square test | Float conversion can lose integer precision or overflow | Use math.isqrt and compare root * root with the input |
| Input read with input() | Text must be converted to a number first | Convert with float() or int(), then validate |
I passed -9 to math.sqrt() and received ValueError: expected a nonnegative input, got -9.0. Handle the exception type rather than matching its message, which can vary across Python versions.
For integer input, math.isqrt(n) gives the exact floor root, and r * r == n checks whether that root squares back to the input.
Floating-point results approximate many roots because their decimal expansions cannot always be represented exactly. Stay with integer operations when exact integer behavior matters. Python’s decimal.Decimal type provides its own square-root method when decimal precision matters.
Conclusion
Check the returned type before using a square-root result in later arithmetic. That matters when the surrounding calculation expects an integer rather than a float.
FAQ
These short answers clarify common method choices.
How do I calculate a square root in Python?
Call math.sqrt(number) for a nonnegative number number. Use cmath.sqrt for a complex result or math.isqrt for an integer floor root.
How can I calculate a square root without math.sqrt?
Raise a nonnegative number to the power 0.5 with number ** 0.5 or pow(number, 0.5).
Why does math.sqrt raise an error for a negative number?
math.sqrt handles nonnegative inputs. A negative input raises ValueError; use cmath.sqrt when you want a complex result.
How do I get an integer square root in Python?
Use math.isqrt(n), which returns the floor of the square root for a nonnegative integer.


