29
loading...
This website collects cookies to deliver better user experience
pow(x,y) # where y is the power of x
# Using the pow() function
import math
num = float(input(" Enter a number: "))
sqRoot = math.pow(num, 0.5)
print("The square root of a given number {0} = {1}".format(num, sqRoot))
Enter a number: 25
The square root of a given number 25.0 = 5.0
# Using the exponent operator to calculate the square root in Python
def sqRoot(n):
if n < 0:
return
else:
return n**0.5
print(sqRoot(36))
6.0
# Using the sqrt() function to calculate the square root in Python
import math
num = int(input("Enter a number:"))
sqRoot = math.sqrt(num)
print (f"The square root of {num} is " ,sqRoot)
Enter a number:16
The square root of 16 is 4.0
# Using the cmath module to calculate the square root of real or complex numbers in Python
import math
num = eval(input(“Enter a number: “)
num_sqRoot = cmath.sqrt(num)
print(“The square root of {0} is {1:0.3f}+{2:0.3f}j”.format(num, num_sqRoot.real, num_sqRoot.imag))
Enter a number: 4+4j
The square root of (4+4j) is 2.197+0.910j
import cmath
a = -25
print(cmath.sqrt(a))
5j