A number’s square root is a number that multiplied by itself, provides the original number. The square root is a fundamental mathematical concept that is also crucial in many programming languages, such as Python, Java, and C++, for working with mathematical data.
This article discusses various methods to find the square root of a number in Python.
How To Find Square Root in Python?
We can calculate the square root in Python using the methods listed below:
- With sqrt() Method
- With pow() Method
- With the exponent operator
Let’s discuss them one by one:
How to Calculate Square Root Using sqrt() Function?
To find the square root of a number, we can use the sqrt() function. This method takes 1 argument. Provide the number in the argument to calculate its square root. Let’s see some examples:
Example 1: Compute the Square Root of an Integer Using sqrt() Function
To calculate the square root of an integer, we can use sqrt() function as follows:
import math
num = math.sqrt(49)
print(num)
In the above code:
- The math module is imported.
- The square root of 49 is calculated using the sqrt() method.
- To print the result, the print() function is used.
The output of the above code is as follows:
The square root of 16 is displayed on the screen.
Example 2: Find the Square Root of the Float Number Using sqrt() Function
We can also calculate the square root of a float number using sqrt() function.
For instance:
import math
num = math.sqrt(22.5)
print(num)
The output of the above code is shown below:
The square root of 22.5 is displayed on the screen.
Example 3: Determine the Square Root of Zero Using sqrt() Function
The following example finds the square root of zero using the sqrt() function:
import math
num = math.sqrt(0)
print(num)
It can be seen that the square root of 0 is 0.0:
Example 4: The Square Root of a Negative Number Using sqrt() Function
Let’s check an example to compute the square root of a negative number:
import math
num = math.sqrt(-16)
print(num)
The output demonstrates that the sqrt() function throws an error because the square root can only be calculated for positive numbers:
How To Calculate Square Root Using pow() Function?
pow() is a fast method to compute the square root of a number. This method takes 2 parameters, the first parameter is the value in numbers and the second parameter is the power of that value.
For instance:
import math
num = eval(input(" Enter a number: "))
square_root = math.pow(num, 0.5)
print("The square root of a given number {0} = {1}".format(num, square_root))
In the above code:
- The math library is imported.
- The code prompts the user to enter a number.
- To find the square root of the provided number, math.pow() function is used.
- In the last line, the string formatting method is used to display the square root of the number.
The output of the above code is as follows:
It can be seen that the square root of the number is displayed.
How To Calculate Square Root Using Exponent Operator?
The exponent operator is denoted with ** in Python. It performs the same operation as the pow() function.
Consider an example for a profound understanding:
import math
x = eval(input(" Enter a number: "))
square_root = x**0.5
print("The square root of the number is",square_root)
In the above code, every step is the same as the previous one except for the exponent operator which calculates the square root of the provided number.
The output depicts that the square root of 4 is 2.
Conclusion
A number’s square root is a value when multiplied by itself, yields the original number. We can calculate the square root of a number in Python using various methods such as sqrt(), pow(), and the exponent operator. All the methods use different techniques and return the square root of the number. In this guide, different methods of calculating the square root of a number have been discussed.