Cara menggunakan python math square root

Salin contoh data di dalam tabel berikut ini dan tempel ke dalam sel A lembar kerja Excel yang baru. Agar rumus menunjukkan hasil, pilih datanya, tekan F2, lalu tekan Enter. Jika perlu, Anda bisa menyesuaikan lebar kolom untuk melihat semua data.

Data

-16

Rumus

Deskripsi

Hasil

=SQRT(16)

Akar kuadrat dari 16.

4

=SQRT(A2)

Akar kuadrat dari -16. Karena angka negatif, pesan kesalahan #NUM! dikembalikan.

#NUM!

=SQRT(ABS(A2))

Menghindari pesan kesalahan #NUM! dengan menggunakan fungsi ABS terlebih dahulu untuk menemukan nilai absolut -16, lalu mencari akar kuadratnya.

Are you trying to solve a quadratic equation? Maybe you need to calculate the length of one side of a right triangle. For these types of equations and more, the Python square root function, sqrt(), can help you quickly and accurately calculate your solutions.

By the end of this article, you’ll learn:

  • What a square root is
  • How to use the Python square root function, sqrt()
  • When sqrt() can be useful in the real world

Let’s dive in!

Python Pit Stop: This tutorial is a quick and practical way to find the info you need, so you’ll be back to your project in no time!

Free Bonus: Click here to get our free Python Cheat Sheet that shows you the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.

Square Roots in Mathematics

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

You can calculate squares using Python:

>>>

>>> n = 5
>>> x = n ** 2
>>> x
25

The Python

>>> 1 ** 2
1

>>> 2 ** 2
4

>>> 3 ** 2
9
2 operator is used for calculating the power of a number. In this case, 5 squared, or 5 to the power of 2, is 25.

The square root, then, is the number n, which when multiplied by itself yields 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:

>>>

>>> 1 ** 2
1

>>> 2 ** 2
4

>>> 3 ** 2
9

You might have memorized some of these perfect squares when you learned your multiplication tables in an elementary algebra class.

If you’re given a small perfect square, it may be straightforward enough to calculate or memorize its square root. But for most other squares, this calculation can get a bit more tedious. Often, an estimation is good enough when you don’t have a calculator.

Fortunately, as a Python developer, you do have a calculator, namely the !

Remove ads

The Python Square Root Function

Python’s

>>> 1 ** 2
1

>>> 2 ** 2
4

>>> 3 ** 2
9
3 module, in the standard library, can help you work on math-related problems in code. It contains many useful functions, such as
>>> 1 ** 2
1

>>> 2 ** 2
4

>>> 3 ** 2
9
4 and
>>> 1 ** 2
1

>>> 2 ** 2
4

>>> 3 ** 2
9
5. It also includes the .

You’ll begin by importing

>>> 1 ** 2
1

>>> 2 ** 2
4

>>> 3 ** 2
9
3:

>>>

>>> import math

That’s all it takes! You can now use

>>> 1 ** 2
1

>>> 2 ** 2
4

>>> 3 ** 2
9
8 to calculate square roots.

sqrt() has a straightforward interface.

It takes one parameter,

>>> import math
0, which (as you saw before) stands for the square for which you are trying to calculate the square root. In the example from earlier, this would be
>>> import math
1.

The return value of sqrt() is the square root of

>>> import math
0, as a . In the example, this would be
>>> import math
4.

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

The Square Root of a Positive Number

One type of argument you can pass to sqrt() is a positive number. This includes both and types.

For example, you can solve for the square root of

>>> import math
9 using sqrt():

>>>

>>> math.sqrt(49)
7.0

The return value is

>>> math.sqrt(49)
7.0
1 (the square root of
>>> import math
9) as a floating point number.

Along with integers, you can also pass

>>> import math
8 values:

>>>

>>> math.sqrt(70.5)
8.396427811873332

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

>>>

>>> 8.396427811873332 ** 2
70.5

The Square Root of Zero

Even

>>> math.sqrt(49)
7.0
4 is a valid square to pass to the Python square root function:

>>>

>>> math.sqrt(0)
0.0

While you probably won’t need to calculate the square root of zero often, you may be passing a variable to sqrt() whose value you don’t actually know. So, it’s good to know that it can handle zero in those cases.

Remove ads

The Square Root of Negative Numbers

The square of any real number cannot be negative. This is because a negative product is only possible if one factor is positive and the other is negative. A square, by definition, is the product of a number and itself, so it’s impossible to have a negative real square:

>>>

>>> math.sqrt(-25)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

If you attempt to pass a negative number to sqrt(), then you’ll get a

>>> math.sqrt(49)
7.0
7 because negative numbers are not in the domain of possible real squares. Instead, the square root of a negative number would need to be complex, which is outside the scope of the Python square root function.

Square Roots in the Real World

To see a real-world application of the Python square root function, let’s turn to the sport of tennis.

Imagine that Rafael Nadal, one of the fastest players in the world, has just hit a forehand from the back corner, where the baseline meets the sideline of the tennis court:

Cara menggunakan python math square root

Now, assume his opponent has countered with a drop shot (one that would place the ball short with little forward momentum) to the opposite corner, where the other sideline meets the net:

Cara menggunakan python math square root

How far must Nadal run to reach the ball?

You can determine from that the baseline is 27 feet long, and the sideline (on one side of the net) is 39 feet long. So, essentially, this boils down to solving for the hypotenuse of a right triangle:

Cara menggunakan python math square root

Using a valuable equation from geometry, the Pythagorean theorem, we know that a² + b² = c², where a and b are the legs of the right triangle and c is the hypotenuse.

Therefore, we can calculate the distance Nadal must run by rearranging the equation to solve for c:

Cara menggunakan python math square root

You can solve this equation using the Python square root function:

>>>

>>> a = 27
>>> b = 39
>>> math.sqrt(a ** 2 + b ** 2)
47.43416490252569

So, Nadal must run about 47.4 feet (14.5 meters) in order to reach the ball and save the point.

Conclusion

Congratulations! You now know all about the Python square root function.

You’ve covered:

  • A brief introduction to square roots
  • The ins and outs of the Python square root function, sqrt()
  • A practical application of sqrt() using a real-world example

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

Mark as Completed

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: The Square Root Function in Python

🐍 Python Tricks 💌

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Cara menggunakan python math square root

Send Me Python Tricks »

About Alex Ronquillo

Cara menggunakan python math square root
Cara menggunakan python math square root

Alex Ronquillo is a Software Engineer at thelab. He’s an avid Pythonista who is also passionate about writing and game development.

» More about Alex


Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Cara menggunakan python math square root

Aldren

Cara menggunakan python math square root

Geir Arne

Cara menggunakan python math square root

Joanna

Master Real-World Python Skills With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

Master Real-World Python Skills
With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

Tweet Share Share Email

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. and get answers to common questions in our support portal.