Python provides a multitude of tools for mathematical computations due to its simplicity and versatility. The square root of a number is one of the basic operations in mathematics. This tutorial will cover several approaches for How to do Square Root in Python, suitable for both novices and experts.
Understanding Square Root in Python
Before getting into the code, let’s first review what a square root is. A number’s square root in mathematics is the value that yields the original number when multiplied by itself. For instance, since 3 * 3 = 9, the square root 9 is 3. Similarly, 4 is the square root of 16 since 4 * 4 = 16. You may have noticed that we only give examples with positive numbers. This is because real numbers in this tutorial have non-negative square roots, which is why we are working with them.
Python’s Built-In “Math” Library
The built-in math library in Python includes several mathematical functions, one of which is for calculating a number’s square root. The square root of a given number can be obtained using the math library’s sqrt() function. The sqrt() function can be used to determine a number’s square root in the following ways:
import math
number = 9
square_root = math.sqrt(number)
print(f”The square root of {number} is {square_root}.”)
Output:
The square root of 9 is 3.0.
How to do Square Root in Python Using the pow() function
In Python, the square root can be quickly determined using the pow() function.
First, let’s examine how Python’s pow() function operates.
The first parameter used by the pow() function is the numerical value, and the second is the numerical value’s power.
Input:
# 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))
Output:
Enter a number: 25
The square root of a given number 25.0 = 5.0
How to do Square Root in Python using the exponent operator
The square root operation is carried out by the exponential operator, **, in the same way as by the pow() function. Let’s define our function and use it to find the square root of a given number to add some intrigue.
Input:
# 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))
Output:
6.0
How to do Square Root in Python using the cmath Module
In Python, the square root of a real or complex number can be found using the cmath module. All positive real numbers can be handled with ease using the different techniques we have already tried. However, the cmath module works well for complex or negative numbers.
Input:
# 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.image))
Output:
Enter a number: 4+4j
The square root of (4+4j) is 2.197+0.910j
Square Root Using the Babylonian Method
The square root of a number can be found using an old algorithm called the Babylonian method, sometimes referred to as Heron’s method. The process begins with a rough estimate and iteratively improves it until the estimate approaches the true square root.
This is the Babylonian method’s algorithm:
- Make a first guess of x0.
- Use the following formula to determine the next guess, x1: x1 = (x0 + number/x0) / 2.
- Until the difference between the current guess and the previous guess is less than a given tolerance value, repeat step 2 using the new guess, x1.
Let’s now put the Babylonian method into practice in Python:
def babylonian_method(number, tolerance=1e-10):
if number < 0:
raise ValueError(“Square root not defined for negative numbers.”)
guess = number
while abs(guess * guess – number) > tolerance:
guess = (guess + number/guess) / 2
return guess
number = 25
square_root = babylonian_method(number)
print(f”The square root of {number} is {square_root}.”)
Comparing the Approaches
- math.sqrt(): The simplest and most direct method is this one. Due to its built-in, performance-optimized function, it is also the fastest. When you need to quickly find a number’s square root and don’t need to implement the algorithm yourself, use this method.
- Exponentiation operator: This method is also straightforward to comprehend. Though not quite as quick as math.sqrt(), it’s still quite good for most applications. When you need a one-liner solution that doesn’t require importing any external libraries, use this method.
- Babylonian method: Compared to the other two methods, this one is slower and more complex. On the other hand, it can aid in your comprehension of the fundamental ideas and methods behind how to find a number’s square root. If you wish to understand more about the mathematics involved in finding square roots, or if you need to implement a custom square root algorithm, use this method.
Conclusion
Because of Python’s extensive library and built-in functions, calculating square roots is a simple task. Python provides tools to meet your needs, regardless of whether you favor the elegance of exponentiation, the depth of iterative algorithms like Newton’s method, or the simplicity of the math module. To gain a deeper understanding of Python’s mathematical computations, experiment with these techniques and conduct additional research. Have fun with coding!