Calculate Square Root in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

sqrt() in Python is an inbuilt function in the math library. It is used to calculate the square root of any number. It takes a number as a parameter and returns the square root of the number we have taken as input.

Introduction

The square root of a number is a value that, when multiplied by itself, results in the original number. It is a commonly used mathematical operation in various fields such as geometry, engineering, and physics. Suppose x is the square root of y, then it is represented as x=√y. Here, is the radical symbol used to represent the root of numbers. In Python, the square root can be calculated using the math module's sqrt() function. This function takes a positive number as an argument and returns its square root as a floating-point value. In this tutorial, we will discuss how to use the sqrt() function in Python to calculate the square root of a number and understand the various nuances involved.

What is the Square Root of a Number?

The square root of a number is that value, which on multiplication by itself, gives the same number whose square root is calculated. The square root of a number is the reverse method of squaring a number.

For example, the square of 7 is 49 and the square root of 49 is 77(√49=7)49 = 7).

Syntax of Python sqrt() Function

For using the square function in Python (sqrt()) we need to import the inbuilt math library into our project.

The square root function in Python has the following syntax:

Where a is the number whose square root has to be calculated.

Domain of Python sqrt() Function

The square root function takes a number as its parameter, and the number should always be non-negative(number 0 & all positive numbers, including floating numbers). If we pass a negative number as a parameter, it will give a math domain error because a complex number is not supported in Python.

Return Type of Python sqrt() Function

The square root function in Python returns float type value for every kind of non-negative number, which is passed as its parameter.

For example, if a perfect square number like 25 is passed in it, it will return 5.0, while on the other hand, if 50 is passed in it, it will return 7.07.

Various Ways to Calculate Square Root in Python

Using sqrt() Function

For finding square root in Python using sqrt(), we need to import the math library into our project. For importing the math library, we use the following line of code:

We pass a number whose square root is to be calculated as a parameter into the sqrt().

Example

Now calculate the square root of number 81 using the sqrt() function of the math library in python.

Code:

Output

Explanation:

  • We imported the math library which has sqrt() in it.
  • We stored the number whose square root is to be calculated into a variable.
  • We stored the result of sqrt() into a variable.
  • Finally, we print the variable to get the square root of the number entered.

Using pow() Function

There is one more function of the math library in Python that helps us to find square root in Python, i.e., power function pow().

This function takes two parameters:

  1. Number of which we want to find the square root
  2. Power which is to be raised on a number

Syntax

Example

This returns 10210^2 which is 100.

So we are now familiar with how we can use the pow() function.

Let's move forward to illustrate how we can find the square root of a number with the help of pow().

Example

Now calculate the square root of number 169 using the pow() function of the math library in Python.

We can pass 0.5 or 1/2 as a parameter into the pow() to find the square root of the function.

Code:

Output:

Explanation:

  • We imported the math library, which has sqrt() in it.
  • We stored the number whose square root will be calculated into a variable.
  • We know that if a number has a power of 0.5, it represents that number's square root. So the number0.5{number}^{0.5} will give us the square root of that number, and its result is stored in the variable named as squareRootOfNumber.
  • Finally, we print the variable to get the square root of the entered number.

Using ** Operator

The operator (**) is termed an Exponentiation operator. This is used to calculate the value of a number raised to a given power.

The value on the right side of Exponentiation operator is considered as power and the value on the left side of Exponentiation operator is considered as a number on which power is to be applied.

Example:

Output:

Explanation:

  • We stored the number whose square root is to be calculated into a variable.
  • We know that if a number has power 0.5, then it represents the square root of that number. So the number**0.5 will give us the square root of that number and its result is stored in a variable named as squareRootOfNumber.
  • Finally, we print the variable to get the square root of the entered number.

Preferred Method to Find Square Root in Python

Using Python 2.7.16

On running the sample test case to find the square root of 1069.69, 10000000 times in Python version 2.7.16. The following results were obtained on finding the square root of a number using the above three methods:

MethodTime Taken
math.sqrt()0.543735271683627
math.pow()2.7523691482161328
** operator1.60870072153690586

Using Python 3.8.2

On running the sample test case to find the square root of 1069.69, 10000000 times in Python version 3.8.2. The following results were obtained on finding the square root of a number using the above three methods:

MethodTime Taken
math.sqrt()0.948235034942627
math.pow()1.2357196807861328
** operator0.40870046615600586

Running the same test case on two different versions of Python (2.7.16 & 3.8.2), we saw that the sqrt() function & ** operator of the math library are efficient. But, it purely depends on the user on which version of Python their machine is working with, as for python version 2, sqrt() function seems to be faster whereas for python3 exponentiation operator (**) is the convenient way for finding the square root.

Nevertheless, the sqrt() function of the math library in Python is the most preferred, dedicated, and convenient way to find the square root of a number as compared to other methods( pow() & ** ).

We can also conclude from the above two examples that the sqrt() function and exponentiation operator(**) are always better than the pow() function of the math library in terms of execution speed.

Example of Square Root in Python

You all have heard about the famous Pythagoras Theorem theorem, which is used to find one side of the right-angled triangle if we have other sides. This famous theorem also uses the square root function to calculate one of the remaining sides.

According to Pythagoras' theorem,

Let's say we have a right-angled triangle whose Hypotenuse is 13 units and Base is 12 units. Now we have to find the triangle's height using this Pythagoras theorem.

Pythagoras Theorem

The modified Pythagoras theorem formula used here for finding the height of the triangle is:

Now, let's see the code for this:

Code:

Output:

Explanation:

  • We imported the math library, which has sqrt() in it.
  • Then, we assign the value of Hypotenuse & Base to the variable.
  • After assigning the value, we find the square of those values and stored them in the new variables.
  • Then, we use the modified Pythagoras theorem formula for finding the value of the Height of the triangle.
  • Finally, we print the desired output.

Conclusion

  • The square root of a number in Python can be calculated using the math module's sqrt() function.
  • The math module provides various mathematical functions and constants.
  • To use the sqrt() function, the math module must be imported.
  • The sqrt() function takes a positive number as an argument and returns its square root as a float.
  • Only positive numbers can be used as input to the sqrt() function, negative numbers result in a ValueError.
  • Complex numbers can be used in Python to calculate the square root of negative numbers.
  • The sqrt() function is a useful tool for performing mathematical operations in Python programs.