Watch Now This tutorial has a associated video course produced by the Real Python team. Watch it along with the composed tutorial to deepen your understanding: The Square Root Function in Python


Are you trying to fix a quadratic equation? Maybe you should calculate the length of one side of a right triangle. For these kinds of equations and also even more, the Python square root attribute, sqrt(), can help you easily and also accurately calculate your services.

You are watching: How to square a number in python

By the finish of this short article, you’ll learn:

What a square root isHow to use the Python square root function, sqrt()When sqrt() have the right to be beneficial in the genuine world

Let’s dive in!


Python Pit Stop: This tutorial is a quick and also practical way to uncover the details you need, so you’ll be earlier to your project in no time!


Free Bonus: Click right here to acquire our free Python Cwarm Sheet that reflects you the basics of Python 3, favor working via data species, dictionaries, lists, and also Python features.


Square Roots in Mathematics

In algebra, a square, x, is the result of a number, n, multiplied by itself: x = n²

You deserve to calculate squares making use of Python:


The Python ** operator is supplied for calculating the power of a number. In this instance, 5 squared, or 5 to the power of 2, is 25.

The square root, then, is the number n, which as soon as multiplied by itself returns the square, x.

In this example, n, the square root, is 5.

25 is an example of a perfect square. Perfect squares are the squares of integer values:


You could have actually memorized some of these perfect squares as soon as you learned your multiplication tables in an elementary algebra class.

If you’re offered a tiny perfect square, it may be straightforward sufficient to calculate or memorize its square root. But for a lot of various other squares, this calculation can obtain a little bit even more tedious. Often, an estimation is good enough once you don’t have actually a calculator.

Fortunately, as a Python developer, you carry out have a calculator, namely the Python interpreter!


The Python Square Root Function

Python’s math module, in the traditional library, can help you job-related on math-connected troubles in code. It includes many valuable features, such as remainder() and also factorial(). It also includes the Python square root feature, sqrt().

You’ll begin by importing math:


That’s all it takes! You have the right to now usage math.sqrt() to calculate square roots.

sqrt() has actually a straightforward interface.

It takes one parameter, x, which (as you witnessed before) represents the square for which you are trying to calculate the square root. In the instance from earlier, this would be 25.

The return value of sqrt() is the square root of x, as a floating allude number. In the example, this would be 5.0.

Let’s take a look at some examples of exactly how to (and also exactly how not to) usage sqrt().

The Square Root of a Confident Number

One type of debate you have the right to pass to sqrt() is a positive number. This includes both int and float kinds.

For example, you deserve to solve for the square root of 49 utilizing sqrt():


The rerotate worth is 7.0 (the square root of 49) as a floating allude number.

Along with integers, you can also pass float values:


You deserve to verify the accuracy of this square root by calculating its inverse:


The Square Root of Zero

Even 0 is a valid square to pass to the Python square root function:


While you most likely won’t should calculate the square root of zero frequently, you may be passing a variable to sqrt() whose value you don’t actually recognize. So, it’s excellent to know that it deserve to take care of zero in those situations.


Rerelocate ads

The Square Root of Negative Numbers

The square of any real number cannot be negative. This is bereason an unfavorable product is only possible if one element is positive and the various other is negative. A square, by interpretation, is the product of a number and itself, so it’s difficult to have actually a negative genuine square:


If you attempt to pass an adverse number to sqrt(), then you’ll get a ValueError because negative numbers are not in the domajor of possible genuine squares. Instead, the square root of an unfavorable number would certainly should be complicated, which is outside the scope of the Python square root function.

Square Roots in the Real World

To view a real-civilization application of the Python square root attribute, let’s turn to the sport of tennis.

Imagine that Rafael Nadal, among the fastest players in the human being, has just hit a forehand from the back edge, wright here the baseline meets the sideline of the tennis court:

*

How far must Nadal run to reach the ball?

You have the right to recognize from regulation tennis court dimensions that the baseline is 27 feet long, and the sideline (on one side of the net) is 39 feet long. So, basically, this boils dvery own to resolving for the hypotenuse of a ideal triangle:

*

You have the right to deal with this equation utilizing the Python square root function:


So, Nadal have to run around 47.4 feet (14.5 meters) in order to reach the sphere and save the point.

See more: How Do You Say Yes You Can In Spanish ? How To Say Yes We Can In Spanish

Conclusion

Congratulations! You currently understand all about the Python square root function.

You’ve covered:

A brief introduction to square rootsThe ins and outs of the Python square root feature, sqrt()A helpful application of sqrt() utilizing a real-human being example

Knowing just how to usage sqrt() is only fifty percent the battle. Understanding when to usage it is the various other. Now, you know both, so go and also use your newfound mastery of the Python square root function!