Club.noww.in
While working with mathematical operations in Python, one of my team members was required to calculate the square root of a number in Python. I thought of writing a complete tutorial. In this tutorial, I will discuss everything about square root in Python with examples.
To calculate the square root in Python, you can use the math.sqrt() function from the built-in math module. First, import the module using import math, then call math.sqrt(x) where x is the non-negative number you want the square root of. For example, math.sqrt(25) will return 5.0.
What is a Square Root in Python?
A square root of a number ( x ) is a number ( y ) such that ( y^2 = x ). In other words, it is a value that, when multiplied by itself, gives the original number. For example, the square root of 16 is 4 because ( 4 \times 4 = 16 ).
There are multiple ways to find the sqrt of a number in Python:
- Using math.sqrt() function
- Using exponentiation operator
Check out Find the First Number in a String in Python
Python math.sqrt() Function
Python provides a built-in module called math that includes a function for calculating the square root. The math.sqrt() function is the most efficient method for finding the square root in Python.
Syntax
Here is the syntax of the sqrt() function in Python.
import math result = math.sqrt(x)The math.sqrt() function takes a single argument x and returns the square root of x. The argument must be a non-negative number; otherwise, it will raise a ValueError.
Example
Now, let me show you an example of finding the sqrt of a number in Python.
Let’s calculate the square root of 25; you can use the below Python code.
import math number = 25 result = math.sqrt(number) print(f"The square root of {number} is {result}")Output:
The square root of 25 is 5.0I executed the above Python code using VS code and you can see the output in the screenshot below:
Check out How to Find the Largest and Smallest Numbers in Python?
Using Exponentiation Operator
Another way to calculate the square root in Python is by using the exponentiation operator **. This method is also simple and effective.
Syntax
Here is the syntax:
result = x ** 0.5By raising a number to the power of 0.5, you can obtain its square root. This method can be particularly useful for quick calculations without importing any modules.
Example
Now, let me show you an example.
Let’s calculate the square root of 49:
number = 49 result = number ** 0.5 print(f"The square root of {number} is {result}")Output:
The square root of 49 is 7.0Here is the output in the screenshot below:
Read Sum of Digits of a Number in Python
Handle Complex Numbers
When dealing with negative numbers, the square root calculation results in a complex number. Python’s cmath module can handle these cases.
Syntax
Here is the syntax:
import cmath result = cmath.sqrt(x)The cmath.sqrt() function returns the square root of a number, including complex results for negative inputs.
Example
Now, let me show you an example.
Let’s calculate the square root of -16:
import cmath number = -16 result = cmath.sqrt(number) print(f"The square root of {number} is {result}")Output:
The square root of -16 is 4jHere is the exact output in the screenshot below:
Read Write a Python Program to Divide Two Numbers
Square Root in Python – Examples
Now, let me show you two real examples of finding square root in Python.
Real Estate Scenario
Imagine you’re a real estate developer in New York, and you need to calculate the side length of a square plot given its area. Then, you can write the Python code below.
import math area = 10000 # in square feet side_length = math.sqrt(area) print(f"The side length of a square plot with an area of {area} sq ft is {side_length} ft")Output:
The side length of a square plot with an area of 10000 sq ft is 100.0 ftHere is the exact output in the screenshot below:
Finance Scenario
Suppose you’re a financial analyst in San Francisco and need to calculate a stock’s volatility given its variance. Here is the complete Python code.
import math variance = 0.04 volatility = math.sqrt(variance) print(f"The volatility of the stock is {volatility}")Output:
The volatility of the stock is 0.2Conclusion
In this tutorial, I explained how to calculate the square root in Python using different methods, such as the math.sqrt() function, the exponentiation operator, or handling complex numbers with cmath, etc. I have also provided two different real examples related to square root in Python.
You may also like the following tutorials: