Python math.sqrt()
The Python .sqrt() function is used to calculate the square root of a given number and is a part of the math library. If the number is negative, the function raises a ValueError.
Python .sqrt() Syntax
import math
math.sqrt(num)
Parameters:
num: The number whose square root is to be calculated.
Return value:
Returns a floating-point number representing the square root of num.
Example 1: Using Python .sqrt() with an Integer
This example uses Python .sqrt() to calculate the square root of an integer:
import mathprint(math.sqrt(4))
Here is the output:
2.0
Example 2: Using Python .sqrt() with a Float
This example uses Python .sqrt() to calculate the square root of a float:
import mathprint(math.sqrt(6.25))
Here is the output:
2.5
Codebyte Example: Handling Multiple Values Using Python .sqrt()
This codebyte example uses Python .sqrt() to calculate the square roots of multiple values:
Frequently Asked Questions
1. What type of value is returned by Python .sqrt()?
The Python .sqrt() function always returns a float, even if the input is an integer.
2. What does “sqrt” stand for?
sqrt stands for square root. It calculates the value that, when multiplied by itself, equals the input number.
3. Is sqrt() a function?
Yes. sqrt() is a built-in function provided by Python’s math module. You need to import the module before using it:
import mathprint(math.sqrt(16)) # Output: 4.0
All contributors
Sriparno08
ishg-153- Anonymous contributor
christian.dinh- Anonymous contributor
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours