Python 3 – Number sqrt() Method
The sqrt()
method in Python 3 is a built-in function that helps to calculate the square root of a specified number. To use the sqrt()
method, you need to import the math
module into your Python program. This article will explain how to use the sqrt()
method in your Python code, with practical examples.
Syntax
The sqrt()
method takes a single argument, which is the number whose square root we want to calculate. The syntax for using the sqrt()
method is as follows:
import math
math.sqrt(number)
Here, number
is the number to whose square root we want to calculate.
Example 1: Using sqrt() Method
import math
# calculate the square root of 16
result = math.sqrt(16)
print("Square root of 16 is: ", result)
# calculate the square root of 25
result = math.sqrt(25)
print("Square root of 25 is: ", result)
# calculate the square root of 36
result = math.sqrt(36)
print("Square root of 36 is: ", result)
Output:
Square root of 16 is: 4.0
Square root of 25 is: 5.0
Square root of 36 is: 6.0
In the above example, we have imported the math
module and used the sqrt()
method to calculate the square root of different numbers. The output shows the square root of each number we asked the program to calculate.
Example 2: Using sqrt() Method in a Function
Suppose that we want to create a function in Python that takes an argument and returns its square root. We can do this by using the sqrt()
method in our function. Here is an example:
import math
def find_square_root(number):
sqrt = math.sqrt(number)
return sqrt
# calculate the square root of 16
result = find_square_root(16)
print("Square root of 16 is: ", result)
# calculate the square root of 25
result = find_square_root(25)
print("Square root of 25 is: ", result)
# calculate the square root of 36
result = find_square_root(36)
print("Square root of 36 is: ", result)
Output:
Square root of 16 is: 4.0
Square root of 25 is: 5.0
Square root of 36 is: 6.0
In the above example, we have created a function find_square_root()
that takes a single argument number
and returns its square root using the sqrt()
method. We then called this function for different values of number
and printed the results.
Example 3: Using sqrt() Method for Complex Numbers
The sqrt()
method can also be used to calculate the square root of complex numbers in Python. Here is an example:
import cmath
# define a complex number
complex_num = 4 + 3j
# calculate the square root
sqrt = cmath.sqrt(complex_num)
# display the result
print("Square root of", complex_num, "is:", sqrt)
Output:
Square root of (4+3j) is: (2+1.0j)
In the above example, we have used the cmath
module to calculate the square root of a complex number. We defined a complex number 4 + 3j
and used the sqrt()
method to calculate its square root. The output shows the result of the calculation.
Conclusion
The sqrt()
method in Python 3 is a useful function that helps to calculate the square root of a specified number. It is easy to use and can be used for both real and complex numbers. By using the sqrt()
method, you can simplify complex calculations and make your Python code more efficient.