How to write square root in Python?

Python has a predefined sqrt() function that returns the square root of a number. It defines the square root of a value that multiplies itself to give a number. The sqrt() function is not used directly to find the square root of a given number, so we need to use a math module to call the sqrt() function in Python.

For example, the square root of 144 is 12.

How to write square root in Python

Using math.sqrt() method

The sqrt() function is an inbuilt function that returns the square root of any number. Following are the steps to find the square root of a number.

  1. Start the program
  2. Define any number whose square root to be found.
  3. Invoke the sqrt() function and pass the value that you have defined in step 2 and store the result in a variable.
  4. Print the square root.
  5. Terminate the program.

Let’s create a Python program to find the square root of a number.

SqrRoot.py

import math # import math module  
N = 25 # define the value to the variable N   
result = math.sqrt(N) # use math.sqrt() function and pass the variable.  
print(" Square root of 25 is :", result) # prints the square root of a given number   
M = 625 # define the value  
result = math.sqrt(M) # use math.sqrt() function and pass the variable  
print(" Square root of 625 is :", result) # prints the square root of a given number   
  
P = 144 # define the value  
result = math.sqrt(P) # use math.sqrt() function and pass the variable  
print(" Square root of 144 is :", result) # prints the square root of a given number   
  
S = 64 # define the value  
result = math.sqrt(S) # use math.sqrt() function and pass the variable  
print(" Square root of 64 is :", result) # prints the square root of a given number  

Output:

How to write square root in Python

Let’s create a python program that finds the square root of a decimal numbers.

SqrRoot.py

import math  
print(" The Square root of 4.5 is", math.sqrt(4.5)) # Pass the decimal number  
print(" The Square root of 627 is", math.sqrt(627)) # Pass the decimal number  
print(" The Square root of 6.25 is", math.sqrt(6.25)) # Pass the decimal number  
  
print(" The Square root of 0 is", math.sqrt(0)) # Pass number as 0  

Output:

How to write square root in Python

In the following program, we have read a number form the user and find the square root.

SqRoot_Usr.py

import math # import math module  
a = int(input("Enter a number to get the Square root")) # take an input   
res = math.sqrt(a) # Use math.sqrt() function and pass the variable a.  
print("Square root of the number is", res) # print the Square Root  

Output:

How to write square root in Python

Using math.pow() function

The pow() is an inbuilt function that is used in Python to return the power of a number. It has two parameters. The first parameter defines the number and second parameter defines the power raise to that number.

Pow_Sqrt.py

import math # import the math module  
num = float(input("Enter the number :")) # take an input  
SquareRoot = math.pow(num, 0.5) # Use the math.pow() function and pass the value and 0.5 (which is equal to √) as an parameters  
print(" The Square Root of the given number {0} = {1}" .format(num, SquareRoot)) # print the Square Root.  

Output:

How to write square root in Python

Using ** Operator

We can also use the exponent operator to find the square root of the number. The operator can be applied between two operands. For example, x**y. It means that left operand raised to the power of right.

Following are the steps to find the square root of a number.

Step 1 . Define a function and pass the value as an argument.

Step 2 . If the defined number is less than 0 or negative, it returns nothing.

Step 3 . Use the exponential ** sign to find the power of a number.

Step 4 . Take the numeric value from the user.

Step 5 . Call the function and store its output to a variable.

Step 6 . Display the Square Root of a number in Python.

Step 7 . Exit from the program.

Let’s implement the above steps in a Python program and calculate the square root of a number.

SqrtFun.py

import math # import the math package or module  
def sqrt_fun(num): # define the sqrt_fun() and pass the num as an argument  
    if num < 0:  # if num is less than 0 or negative, it returns nothing  
        return  
    else:  
        return num ** 0.5 # Use the exponent operator   
num = int(input (" Enter a numeric value: ") ) # take an input from the user  
  
res = sqrt_fun(num) # call the sqrt_fun() to find the result  
print("  Square Root of the {0} = {1}".format(num, res)) # print the Square Root of the variable

Output:

How to write square root in Python

As we can see in the above example, first we take an input (number) from the user and then use the exponent ** operator to find out the power of a number. Where 0.5 is equal to √ (root symbol) to raise the power of a given number.

Let’s create a Python program that finds the square root of between the specified range. In the following program, we have found the square root of all the number between 0 to 50.

Sqrloop.py

import math  
for i in range(50):  
    print("Square root of a number {0} = {1}".format(i,math.sqrt(i)))  

Output: