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:
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.
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.
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:
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:
The sqrt operator works for numbers of any size, which is helpful. Here is an example of sqrt applied to a very large number:
Here is the output
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:
frommathimportsqrt#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:
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:
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: