Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

An Introduction to Square Roots in Math and in Python

00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’ll introduce you to square roots in Python. A square is a number that is multiplied by itself.

00:11 There are many ways of expressing this in math and programming. The big cross here indicates multiplication. In most programming languages, the star (*) is used for the same thing. In algebra, the superscript of 2 means to square the value.

00:27 Some programming languages use the caret symbol (^), or Shift + 6 on a U.S. keyboard, to denote exponents. An exponent, or power of 2, is the same as squaring. In Python, double star (**) is the power operator. **2 is the same as square.

00:46 A square root is the number that when squared gives you the question, “If y is equal to x squared, given y, what would x be?” This is also noted as the square root of y. The little check mark here means to take the square root of what is under it. This symbol, the check mark, is called the radical sign, the radical symbol, the root symbol, the radix, or the surd. Here’s an example.

01:14 5 times 5 is 25, so the square root of 25 is 5.

01:22 Let me start by using the power operator to square the number 3. 3 times 3 is 9, all good. Let me do it once more with 5. 5 squared is 25. Now to get the square root, I need to get the function that does this from the math library.

01:45 The square root of 25 is 5.0. 6 squared is 36, so the square root of 36 is 6.0. So far, I’ve only used integers and what are called perfect squares.

01:59 When you square an integer, the result is a perfect square. When you square root a perfect square, you get back that integer. If you square root a number that isn’t a perfect square, you’ll get an irrational number. In Python, these are represented by floats.

02:16 Seeing as I’ve done sqrt() for 25 and 36, you could guess that the square root of 30 is going to be somewhere between 5.0 and 6.0. Let’s see that. And there you go!

02:28 5.47 et cetera. You can see how well this worked by storing it and then squaring the result. Back to 30.0. That worked out well.

02:44 The square root of 0 is 0.0. If you think about it for a moment, that makes sense. 0 times 0 is 0, so the thing you multiply by itself to get 0 is 0. It feels kind of zen.

02:56 You’re not likely to need this directly, but if you’re doing math on a variable and it happens to contain 0, then you know what result to expect. What about negatives?

03:07 Python doesn’t allow this. The square root of a negative number is what’s called a complex number. Python has a method for dealing with complex numbers, but you have to explicitly request that it does so. As I’m just using the vanilla sqrt() here I get a ValueError instead.

03:28 In addition to the sqrt() function, Python 3.8 introduced the isqrt() function in the math module. Now that I’ve imported it, let me run it on 25.

03:39 No problem here. Still in perfect square land. Same thing with 36. And using it on 30 might give you a guess as to what the “i” part in isqrt() stands for. This returns the floor of the square root, giving the integer result.

04:01 In addition to squares, you can also cube something. That’s a number multiplied by itself and then by itself again. Same notations as before, but this time with a 3 instead of a 2.

04:13 The generalization of this is called the power of a number. x to the n-th power is x times x times x, n times. Working that backwards, and you get the n-th root. If y is x to the n, then what is x? To indicate that a root is an n-th root, the n value gets tagged into the front of that root symbol.

04:38 Here’s an example. 4 cubed is 64, so the third root, or cube root, of 64 is 4.

04:50 Python does not have an n-th root function, but that isn’t a problem because of this neat little math trick. The n-th root of something is the same as the value to the power of 1 over n. For instance, the cube root of y is equivalent to y to the power of one third, or 1 over 3. Let’s go back to the REPL and use this.

05:14 Repeating the previous example in the REPL, 4 cubed is 64. You can also use the built-in pow() (power) function to achieve the same thing.

05:25 Or you can use the pow() function from the math module.

05:33 Notice the difference. The one in the math module always returns a float. Now let’s use that fraction trick.

05:42 25 to the power of 1 over 2—that’s a half—is 5. See? Square root! Notice that I wrote 1. inside of the exponent. That is to force Python to treat that number as a float.

05:57 Now let’s use the fractional power to cube root 64.

06:04 And that’s pretty close, but not quite right. This has nothing to do with the formula for the cute root. It has to do with how float numbers are stored in a computer.

06:13 You’ll find a lot of float numbers do weird things when division and fractions are involved. You’ll get little pieces missing. You have to be particularly careful with this when doing a lot of math, as the errors can compound over multiple uses. A little error at the beginning fed into another formula can make you a fair ways off at the end.

06:32 That’s just the hazards of doing math with floats. For completion’s sake, let me just show you the pow() function from the math library to do the same thing.

06:45 Not surprisingly, you get the same result.

06:50 Next up, how computers calculate these roots.

Become a Member to join the conversation.