Kodeclik Blog


Python square root

Square root function in Python

Python square root, sqrt(), is a built-in function in Python that you can use to return the square root of a number. To compute square roots, you first import the math library and then invoke the sqrt() function in this library.

What is a square root?

The square root is the inverse of the square function. Just like the inverse of the relationship “parent” is the “child” relationship. In other words, we are interpreting the relation between the two entities in the opposite direction.
For instance, 3 squared is 9. The square root of 9 is 3. Well that is not quite right. The square root of 9 can also be -3 (because -3 squared is also 9). But for simplicity sake we say that the square root of 9 is 3 (with the implicit understanding that it could also be negative).

How to square a number in Python

Before we see how to compute square roots, let us refresh our memory on how we compute squares. There are at least three ways to do so: we can use the exponentiation operator ("**"), the in-built pow() function, or the pow() function that is imported from the math module, i.e., the math.pow() function.

Python square roots with sqrt()

The Python programming language has a Math library that comes with a built-in function in Python, sqrt(), to calculate square roots. The first step is to import the Math module in Python. After importing, you are ready to use it!
The result is:
Note that sqrt() gives the output as a floating point number, which you can convert to integer if necessary. But you have to remember that there are integers for which the square root will be a floating point number. For instance if your input is not a perfect square:
The output is:

How to test if a number is a perfect square

How can we tell if a number is a perfect square? Simple - we find the square root, convert it to an integer, square it again and see if we get the original number:
The output is:

Does sqrt() understand complex numbers?

In other words, does the sqrt() function in Python return the square root of a negative number? Let's see what happens when we print the square root of a negative number.
We know that the square root of -1 is the imaginary number often denoted by “i”, but the math module’s sqrt() function does not work with imaginary numbers. So the above code will output:
If you would like to find square roots of negative numbers, you can explore the use of the "cmath" module. The cmath module is a built-in module in Python that contains a variety of complex mathematical functions. In the below program, we first import the cmath module and then use the square root function within this library (which is also called sqrt, but not to be confused with the math module).
The output is:
where the imaginary number is denoted by the letter "j".
But let us get back to our traditional case where we are given a positive number and we know that the output is going to be a real number.

Finding the hypotenuse of a right triangle

According to the Pythagorean theorem, in a right triangle, the sum of the squares of the lengths of the two sides is the square of the hypotenuse. So if we know the two smaller sides of a right triangle, we can compute the hypotenuse as follows:
This yields:
as expected because 3^2 + 4^2 = 5^2.

Using sqrt() to check if a triangle is a right triangle

We can use the same logic to test if a triangle is a right triangle:
This yields:
The third answer (False) is because the triangle is not a right triangle. The fourth answer is given as False because we didn’t give the sides in the correct order. If we wanted this to work for any order, we should take the largest value and test if it is the square of the sum of the squares of the other two sides.
Notice that we have not really used the sqrt() function in the above example. We can use it in the following manner:
This should yield the same output as above although it is bad programming practice to test for equality between floating point numbers (recall that sqrt will return floating point numbers). The right way to do it would be to see if the difference between the two sides is less than our precision tolerance. (This is left to the reader as an exercise).

Finding Python square roots in surd notation

What if we don’t care about floating point numbers and instead would like to report square roots as irrational numbers? In other words, we desire to write a function that returns the square root of a number in surd notation. For the input of 2, we would like the square root to be printed as:
and the square root of 20 to be printed as:
This is called “surd notation”. Let us write a program that requests a number from the user as input and prints out the square root in surd notation.
Note how the code is organized. First we have two helper functions which are self-explanatory. Is_factor tests if a number is a factor of another. Is_square_number tests if the input number is a square.
A lot of the magic happens in the next two functions. find_greatest_square_factor tries to find a highest square that is a factor of the original number For this purpose, it starts with half of the given number (because the factor cannot be higher than the square root of this). Then it progressively decreases this estimate to find the largest square.
This function is then used in the main driver function reduction_of_the_square. Here we test for two cases, whether the given number is a square or not. If it is, then we have found the square root as a perfect integer. If not, we factorize out the largest square and place the remaining element under the square root symbol.
Finally, we ask the user for an input number and print the square root in surd notation.
Here is a sample run:
If you liked learning about square roots in Python, checkout our blogpost on the Python pow() function.
Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares and about the Python isnumeric() method. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2022. All rights reserved.