How to Find the Square Root In Python?

Last updated on: by Digamber
In this Python tutorial, we will understand how to calculate the square root of a specified numerical value using the exponent operator. I will share with you real-world examples to find out a square root in Python.

Get Square Root in Python 3 without Built-in Function

In the first example, we are going to calculate the square root in Python without using any built-in Python function. We’ll use a simple math technique to get the square root, check out the example below.

num = int(input("Add a number to get square root: "))
sqrt = num ** 0.5
print("square root:", sqrt)

You can check out the square root output for below numerical values:

# Add a number to get square root: 5
square root: 2.23606797749979
# Add a number to get square root: 10
square root: 3.1622776601683795
# Add a number to get square root: 20
square root: 4.47213595499958
# Add a number to get square root: 50
square root: 7.0710678118654755

Calculate the Square root using Math Module in Python

Now, we will find out the square root using built-in sqrt() function. This function is straightforward to use; it does the calculation job automatically. To make the math.sqrt() method works smoothly, import the the math module first and then define the numerical value inside the sqrt() method. Lastly you can use print() method to get the square root. Check out the example below.

import math
num = int(input("Add a number to get square root:"))
sqrt = math.sqrt(num)
print("Square root is:" , sqrt)

Following will be the output for math.sqrt() programme:

# Square root is: 50
7.0710678118654755
# Square root is: 90
9.486832980505138
# Square root is: 150
12.24744871391589

Find Square Root Using Pow() Method in Python 3

In the next example, we are going to use the pow() built-in method to calculate the square root in Python 3.

Let us understand how does pow() function work in Python.

The pow() method takes 2 parameters, the first parameter is the numerical value, and the second parameter is the power of the numerical value. If we have a look at it, then you’ll notice that it is similar the way we have calculated the square root in the above examples.

import math
num = int(input("Add a number to get square root:"))
sqrt = math.pow(num, 0.5)
print("square root is: ", sqrt)

See the output below:

# Add a number to get square root: 5
square root: 2.23606797749979
# Add a number to get square root: 10
square root: 3.1622776601683795
# Add a number to get square root: 20
square root: 4.47213595499958
# Add a number to get square root: 50
square root: 7.0710678118654755

Digamber

I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.