Square root in Python
In this post, you will learn different ways to find the square root using the Python programming language. In mathematics, the square root is a factor of a number that, when multiplied by itself, gives the original number. For example, the square root of 9, √9 = 3. Such a type of question is generally asked in programming interviews.
Find square root using exponent
The exponentiation operator (**) returns the result of raising the first operand to the power of the second operand.
x**n
The operator ** raises the x to the power of an exponent n. We can use the exponent (**) sign to find out the square roots in Python. The following example demonstrates this-
x = int(input("enter a number: "))
sqrt = x ** 0.5
print("square root:", sqrt)
Output of the above code-
Enter a number: 9
Square root: 3.0
Enter a number: 7
Square root: 2.6457513110645907
In the above program, we store the number in the x variable and find the square root using the ** exponent operator.
Python sqrt() method
The sqrt() method returns the square root of a number. To use this method, first we need to import the math module and then we need to call this function using the math static object. It accepts a number as an argument. The number must be greater than or equal to 0. If the number is less than 0, it returns a ValueError. If the value is not a number, it returns a TypeError.
import math
# Return the square root of different numbers
print(math.sqrt(9))
print(math.sqrt(25))
print(math.sqrt(16))
print(math.sqrt(30))
print(math.sqrt(5.9))
print(math.sqrt(3.7))
print(math.sqrt(13.65))
Output of the above code -
3.0
5.0
4.0
5.477225575051661
2.4289915602982237
1.9235384061671346
3.6945906403822333
Python pow() method
The predefined method pow() is also used to find out the power of a number. Before using this method, we need to import the math module. This function takes two arguments as input, first is the number itself, and the second one is the power of that number.
import math
num = int(input("Enter a number:"))
sqrt = math.pow(num, 0.5)
print("square root: ", sqrt)
Output of the above code -
Enter a number:7
square root: 2.6457513110645907
Enter a number:8
square root: 2.8284271247461903
Find the square root of real or complex numbers
By importing the complex math module, we can find the square root of real and complex numbers in C programming language.
import cmath
num = 1+2j
sqrt = cmath.sqrt(num)
print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,sqrt.real,sqrt.imag))
Output of the above code:
The square root of (1+2j) is 1.272+0.786j
Related Articles
find average of n numbers in Python
Multiply all elements in list Python
Remove last element from list Python
Find the stop words in nltk Python
Python multiline string
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python compare two lists
Insert data in MySQL database from an HTML form using Django
Alphabet pattern programs in Python