Python square root, sqrt(), is a built-in function in Python that you can use in your calculations within your programs. As you know, 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 use 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.
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 “i”, but the math module’s sqrt() function does not work with imaginary numbers. So the above code will output:
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:
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:
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? For instance we would like the square root of 2 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.
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.