How To Use the Python Square Root Function

There are many situations in which you will want to find the square root of a function in a Python application.

Fortunately, Python includes some very powerful functionality for calculating square roots.

In this article, I will teach you how to use the Python square root function. I will also show you how to calculate square roots without this function, and how to calculate the square root of every element in an outside data structure.

Table of Contents

You can skip to any particular section of this Python tutorial using the links below:

What is a Square Root?

It’s hard to understand the square root without understanding squares first.

In mathematics, the square of a number is the value that is generated when you multiply a number by itself. Here are a few examples:

  • The square of 2 is 4, since 2 times 2 is 4
  • The square of 4 is 16, since 4 times 4 is 16
  • The square of 8 is 64, since 8 times 8 is 64

In Python, these values are easy to calculate using the ** operator, which is used to calculate exponents.

The same examples that I used above are calculated using Python in the following code block.


2**2

#Returns 4

4**4

#Returns 16

8**8

#Returns 64

The square root function is almost like a backwards version of the square. It is the number that when multiplied against itself yields the square value.

A few examples (using the same mathematical relationships from before) are below:

  • The square root of 4 is 2, since 2 times 2 is 4
  • The square root of 16 is 4, since 4 times 4 is 16
  • The square root of 64 is 8, since 8 times 8 is 64

These square roots were fairly easy to determine since they were small enough integers to be included in our elementary school times tables.

For larger numbers, it can be very difficult to calculate square roots. Fortunately, the Python square root function exists to make our life easy here. We’ll learn about the Python square root function in the next section.

Return to the Table of Contents

The Python Square Root Function

Python comes with a built-in math module that contains many useful functions. One of these functions is sqrt, which allows us to calculate square roots.

To use this function, you must first import the math module into your application with the following command:


import math

Now that the math module has been imported, we can call the sqrt operator from the math module using the dot operator. As an example, here’s how you would use the sqrt function to compute the square root of 4:


math.sqrt(4)

#Returns 2.0

The sqrt operator works for numbers of any size, which is helpful. Here is an example of sqrt applied to a very large number:


math.sqrt(68564654987654321654984.3215)

Here is the output


261848534438.62222

Depending on the purpose of your Python program, you may want to import exclusively the sqrt function and not the entire math module.

Here is how you modify your module import:


from math import sqrt

#instead of 'import math'

Since you did not import the entire math module, you do not need to call the sqrt function from the math module using the dot operator. Instead, you can call the sqrt function directly, like this:


sqrt(100)

Here is the output:


10.0

Return to the Table of Contents

Calculating the Square Root of Every Element in a Python List

Let’s say you had a Python list containing numbers, and you wanted to calculate the square root of every element in the list.


my_list = [1, 4, 9, 16]

Can you use the sqrt function to do this?

Let’s try it:


from math import sqrt

my_list = [1, 4, 9, 16]

sqrt(my_list)

Unfortunately, this will return ` TypeError` that looks like this:


TypeError: must be real number, not list

It is clear that the sqrt function is not designed to work with lists. It is still possible to calculate the square root of every element in a list.

To do this, we will need to use a loop. Here’s what this loops like:


from math import sqrt

my_list = [1, 4, 9, 16]

i=0

while i < len(my_list):

    my_list[i] = sqrt(my_list[i])

    i += 1

If you were to print out the my_list list now, you would see that its values have each had the sqrt function applied to them.


[1.0, 2.0, 3.0, 4.0]

Return to the Table of Contents

How To Calculate Square Root Without the Python Square Root Function

It is possible to calculate Python square roots without using the sqrt function.

This is because the square root function is the same as raising a number to the power of 0.5. We have already seen that the ** operator allows us to calculate exponents in Python. Here is how you could calculate the square root of 100 without using the sqrt function:


100**0.5

#Returns 10.0

To go back to our earlier example of computing the square root of the elements within a Python list, here is how you could refactor this code to avoid using the sqrt function:


my_list = [1, 4, 9, 16]

i=0

while i < len(my_list):

    my_list[i] = my_list[i]**0.5

    i += 1

my_list

Return to the Table of Contents

Final Thoughts

In this tutorial, you learned how to use sqrt, the Python square root function.

I also explained the basics of square roots from a mathematical perspective and showed you how to calculate square roots in Python without the math module.

I hope you enjoyed this tutorial. If you have any ideas or suggestions for future content, please email me!

If you enjoyed this article, be sure to join my Developer Monthly newsletter, where I send out the latest news from the world of Python and JavaScript:
Written on April 30, 2020