An Introduction to Square Roots in Math and in Python
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.
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.
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 squared is
25. Now to get the square root, I need to get the function that does this from the
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.
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
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: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: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.
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.
Become a Member to join the conversation.