Square Root in Python

Python is one of the easy-to-learn programming languages not just because of its simple English syntax but also due to the presence of lots of libraries for different tasks. These are the reasons for Python being so popular among newbies.

Similarly, for doing mathematics tasks, Python has a built-in module called math that is used for doing lots of mathematical calculations in Python programming. See some of the methods of the math module in Python:

MethodDescription
math.ceil()Rounds a number up to the nearest integer
math.floor()Rounds a number down to the nearest integer
math.log()Returns the natural logarithm of a number, or the logarithm of number to base
math.pow()Returns the value of x to the power of y
math.sqrt()Returns the square root of a number
math.sin()Returns the sine of a number
math.cos()Returns the cosine of a number
math.tan()Returns the tangent of a number
math.fsum()Returns the sum of all items in any iterable (tuples, arrays, lists, etc.)

Square Root in Python

In Python math module we have math.sqrt() method that returns the square root of a number provided the number must be greater than or equal to 0. If the number is less than 0, it returns a ValueError. If the value is not a number, it returns a TypeError.

Let’s find the Square root of 25 in Python:

import math
root = math.sqrt(25)
print(root)
Output: 5

As you can see in the above code, we first imported the math module in our Python file.

Let’s find some more square roots:

import math

print (math.sqrt(9))
print (math.sqrt(20))
print (math.sqrt(15.67))
Output: 3
4.47213595499958
3.9585350825778973

Related:

Python Basics
List, Set, Tuple, and Dictionary in Python
Python Reverse List items
Python Round() Function
Python Multiline comment
Power in Python | Python pow()
Python range() Function
Square Root in Python
Python for i in range