math.sqrt() Function Explained
math.sqrt() returns the square root of a number in Python. It is part of the math module, so you need to import that module before using it.
This function is useful when writing formulas, solving geometry problems, or doing basic calculations. On this page, you will learn what math.sqrt() does, what it returns, when to use it, and the most common beginner mistakes.
Quick answer
import math
result = math.sqrt(25)
print(result) # 5.0
Use math.sqrt() after importing the math module. It returns a float.
What math.sqrt() does
- Returns the square root of a number
- Comes from Python’s built-in
mathmodule - You must write
import mathbefore using it - The result is usually returned as a float
A square root is a number that multiplies by itself to make the original number.
For example:
- The square root of
9is3 - The square root of
25is5
If you are new to the math module, see the Python math module overview.
Basic syntax
The syntax is:
math.sqrt(x)
Here:
xshould be a numberxis usually anintorfloat- The function returns the square root of
x
Example:
import math
print(math.sqrt(9))
Output:
3.0
Simple working example
Here is a complete example:
import math
number = 36
result = math.sqrt(number)
print(result)
Output:
6.0
What this code does:
- Imports the
mathmodule - Stores
36in the variablenumber - Calculates the square root with
math.sqrt(number) - Prints the result
What the return value looks like
math.sqrt() returns a float.
That means even if the answer looks like a whole number, Python usually shows it with .0.
Example:
import math
print(math.sqrt(16))
Output:
4.0
This is normal. The function returns 4.0, not 4.
If you need to understand floats better, see Python float() explained and Python numbers explained: int, float, complex.
Only convert the result to an integer if you are sure the square root should be a whole number.
import math
result = math.sqrt(16)
print(int(result))
Output:
4
When to use math.sqrt()
Use math.sqrt() when:
- You want the square root of a positive number
- You are writing math formulas
- You are working with distance, geometry, or basic calculations
Example:
import math
side = 49
length = math.sqrt(side)
print(length)
This is clearer than writing a more complex expression when all you need is a square root.
Negative numbers and errors
math.sqrt() does not support negative numbers in normal real-number math.
Example:
import math
print(math.sqrt(-1))
This raises:
ValueError: math domain error
Why this happens:
math.sqrt()works with real numbers- The square root of a negative number is not a real number
- So Python raises an error instead of returning a result
If this is the error you are seeing, read how to fix ValueError: math domain error.
A simple way to avoid this problem is to check the value first:
import math
value = 25
if value >= 0:
print(math.sqrt(value))
else:
print("Cannot take the square root of a negative number")
Common beginner mistakes
Here are the most common problems when using math.sqrt().
Forgetting to import math
This causes an error because Python does not know what math is.
Wrong:
result = math.sqrt(25)
print(result)
Right:
import math
result = math.sqrt(25)
print(result)
Writing sqrt(9) instead of math.sqrt(9)
If you only write sqrt(9), Python will usually raise a NameError.
Wrong:
import math
print(sqrt(9))
Right:
import math
print(math.sqrt(9))
Passing a string instead of a number
This will fail because "25" is text, not a number.
Wrong:
import math
print(math.sqrt("25"))
You can check the type of a value with:
value = "25"
print(type(value))
print(value)
Right:
import math
value = "25"
number = float(value)
print(math.sqrt(number))
Using a negative value without checking first
If the value might be negative, test it before calling math.sqrt().
import math
value = -9
if value >= 0:
print(math.sqrt(value))
else:
print("Value must be 0 or greater")
Expecting an integer but getting a float
This is normal behavior.
import math
print(math.sqrt(25))
Output:
5.0
Alternatives to math.sqrt()
You can also calculate a square root with x ** 0.5.
Example:
x = 25
print(x ** 0.5)
Output:
5.0
This works, but math.sqrt(x) is often easier for beginners to read. It clearly shows that you want the square root.
FAQ
Do I need to import math to use sqrt()?
Yes. Use import math, then call math.sqrt(number).
Why does math.sqrt(9) return 3.0 instead of 3?
Because math.sqrt() returns a float.
Can math.sqrt() work with negative numbers?
Not in the regular math module for real numbers. It raises ValueError.
Is math.sqrt() better than x ** 0.5?
For beginners, math.sqrt() is often clearer and easier to read.