 Python Program to Find the Square Root

The python sqrt is an inbuilt function in programming language that can be found using the exponent’s operator and the cmath module. The procedure is different for positive numbers and real & complex numbers.

Python sqrt

Example:

Square root of 16 is 4.

The square root of 100 is 10.

Square root of 62 is 8. Source

sqrt python for Positive Numbers

In the program given below, the float integer is stored in the numb variable. The square root of the number is found using the ** operator. The program given below applies to only positive integers.

Source Code:

numb = float(input('Enter a number: '))

numsqrt = numb ** 0.5

print('square root of %0.3f is %0.3f'%(numb ,numsqrt))

Output:

Enter a number

The square root of 8.000 is 2.828

if the value is lesser than 0

Source Code:

import math

#an error will be generated if the value of x is lesser than 0

print(math.sqrt(-1))

sqrt in python for Real or Complex Numbers

In the program discussed below, we use the sqrt() from the cmath module. If the user wants to take the input from the user, the eval method should be used. In Python, the eval() method can be used to convert complex numbers into complex objects.

Source Code:

import cmath

numb = 1+2j

numsqrt = cmath.sqrt(numb)

print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(numb ,numsqrt.real,numsqrt.imag))

Output

The square root of (1+2j) is 1.272+0.786j

How do you do a Square Root in Python?

In Python, the sqrt() is an inbuilt function that is used to return the square root of any number.

The square root function has the following syntax:

math.sqrt(x)

Parameter: x is passed as the parameter to the sqrt(). The condition is that x>0 and x is not 0. If x<0 then the program will generate a runtime error.

Example 1:

import math

#statement to generate the square root of 0

print(math.sqrt(0))

#statement to generate the square root of 4

print(math.sqrt(4))

#statement to generate the square root of 3.5

print(math.sqrt(3.5))

Output:

0.0

2.0

1.8708286933869707

Example 2:

import math

print ("math.sqrt(100) : ", math.sqrt(100))

print ("math.sqrt(7) : ", math.sqrt(7))

print ("math.sqrt(math.pi) : ", math.sqrt(math.pi))

Output:

math.sqrt(100) : 10.0

math.sqrt(7) : 2.6457513110645907

math.sqrt(math.pi) : 1.7724538509055159

What is the Syntax of Sqrt?

The syntax of the sqrt function is:

math.sqrt(x)

Parameter:

x is any number such that x>=0

Returns:

The parameter returns the square root of the number passed.

How do you remove square root in Python?

The following program is used for removing the square root in Python

Source Code

import math

toSquare = 500

squared = math.sqrt(toSquare)

What is NP sqrt in Python?

The numpy.sqrt(array[, out]) method is used to get the element-wise positive square root of an array.

Syntax: numpy.sqrt()

Parameters:

array : [array_like] The square roots of input values must be found.

out : [ndarray, optional] Alternative array object to place the result in; if provided, it must be of the same shape as arr.

Returns : [ndarray] The square root of an integer in an array is returned.

Source Code 1:

import numpy as geek

# applying sqrt() method on integer numbers

array1 = geek.sqrt([1, 4, 9, 16])

array2 = geek.sqrt([6, 10, 18])

print("squareroot of an array1 : ", array1)

print("squareroot of an array2 : ", array2)

Output:

the square root of an array1 : [ 1. 2. 3. 4.]

the square root of an array2 : [ 2.44948974 3.16227766 4.24264069]

Source Code 2:

import numpy as geek

# applying sqrt() method on complex numbers

array2 = geek.sqrt([4, -1, -5 + 9J])

print("square-root of an array : ", array2)

Output:

square-root of an array : [ 2.00000000+0.j 0.00000000+1.j 1.62721083+2.76546833j]

Source Code 3 :

import numpy as geek

# Using the sqrt() on a real integer with a negative element

arr = geek.sqrt([-4, 5, -6])

print("square-root of an array : ", arr)

Output:

RuntimeWarning

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.  Ashhar Firdausi
IIT Roorkee
Biology  Dr. Nazma Shaik
VTU
Chemistry  Gaurav Tiwari
APJAKTU
Physics
Get Started 