In this tutorial, I will show you three different ways to find square root in python with program examples.
In mathematics, a square root is a number when multiplied by itself gives the original number. For example, the square root of 36 is 6. When we multiply 6 twice it gives the original value 36.
Below are some popular methods by which we can calculate the square root of a number in python.
1. Using Exponent
number = int(input("enter a number: ")) sqrt = number ** 0.5 print("square root:", sqrt)
Output:
enter a number: 64
square root: 8.0
In the above program, first, we’re taking a number from the user as input and the entered value will be converted to int from string and will store into a variable called number. Then we are using the exponent (**) sign, which is used to find out the power of a number. But we know that if a number has power ½ or 0.5 then it will represent the square root of that number. That’s why we are using 0.5 as power here. So the number**0.5 will give us the square root of that number and will be stored in a variable named sqrt. In the last line, we’re just printing the square root of the number.
2. Using math.sqrt() Method
import math number = int(input("enter a number:")) sqrt = math.sqrt(number) print("square root:" , sqrt)
Output:
enter a number:36
square root: 6.0
sqrt() is the predefined method used to find square root in python. But we have to import the math module to use the sqrt() method. In the first line, we’re importing the math module, then in the next line, taking input from the user. After that, we’re finding the square root of the number using the sqrt() method and the result will be stored in the sqrt variable. In the last line, just print the square root as in the first program.
3. Using math.pow() Method
import math number = int(input("enter a number:")) sqrt = math.pow(number, 0.5) print("square root: ", sqrt)
Output:
enter a number:81
square root: 9.0
pow() is also a predefined method to find out the power of a number, it takes two arguments as input, the first is the number itself and the second one is the power of that number. The program is the same as the first program where we’re using (**) sign to find out the square root but the only difference is that here we’re using a predefined method pow() instead of (**) sign to get the power of that number.
If you’ve any problem related to the article or have any other possible way to find square root in python then please let us know in the comments.
Another way
Example to find square root of 4
Solution
S=4
Sqrt=//4
Print (sqrt)
It’s an error here.
Invalid syntax:
Every time I attempt to use sqrt in python, an invalid syntax error is thrown at the first parentheses. Any advice would be greatly appreciated.
import math
number = int(input(“entre a value please? “)
sqrt = math.sqrt(number))
print(sqr)
by Awharitoma Kenneth
Write import math first
def is_prime(n):
if n < 2:
return False
import math
for i in range(2,int(math.sqrt(n))+1):
if n % i == 0:
return False
return True
import math
number = float(input(“Enter a number: “))
sqrt = math.sqrt(number)
print(“The square root of”,number,”is”,sqrt)