Learn Python – How to write square root in Python?- Basic and advance

Python has a predefined sqrt() characteristic that returns the square root of a number. It defines the square root of a fee that multiplies itself to give a number. The sqrt() feature is no longer used at once to find the square root of a given number, so we need to use a math module to name the sqrt() function in Python.

For example, the square root of 144 is 12.

Using math.sqrt() method

The sqrt() feature is an inbuilt characteristic that returns the rectangular root of any number. Following are the steps to discover the square root of a number.

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

Let’s create a Python program to find the rectangular 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:

Let’s create a python program that finds the rectangular 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:

In the following program, we have examine a quantity form the consumer and locate 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:

Using math.pow() function

The pow() is an built in feature that is used in Python to return the power of a number. It has two parameters. The first parameter defines the quantity and 2nd parameter defines the electricity increase 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:

Using ** Operator

We can additionally use the exponent operator to locate 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 electricity of right.

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

Step 1. Define a characteristic and ignore the price as an argument.

Step 2 If the defined quantity is much less than zero 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 characteristic 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 put in force the above steps in a Python software and calculate the rectangular 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:

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

Let’s create a Python program that finds the square root of between the targeted range. In the following program, we have found the rectangular root of all the number between zero 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: