In this video we're going to discuss the constants
that are available in Python's "math" module.
That's, pi, tau, e, infinity - positive and
negative - and "not a number." And if you're
not sure what that means, don't worry, we
will explain it.
First things first, though, we will need the
math module. The good news is, you don't need
to download anything, Python's math module
is built in so if you want to use it you just
have to type import math. And there you have
it. The math module comes with these constants
I listed above, I'll start with one that should
be familiar, and that is pi.
Pi has endless digits. Somewhere out there,
there's computers churning away, calculating
more and more and more digits of pi, we don't
want that to be our computer. Our program
would never do anything else, there's a host
of other limitations involved, this is just
going to give you an approximation of pi to
a certain number of digits, like you would
get on any calculator. Let's take a look.
To get the approximation of pi in the math
module, well first let's type math, and that
indicates that you want to use the math module,
then you type dot, and that dot indicates
you want to use a function within the math
module, then you type out the name of the
function you want to use, in this case, pi.
So you type math.pi and let's try printing
that, and see what it gives us.
And indeed it gives you 3.14 and change, it
gives you pi within fifteen digits. You can
also set it as a variable, and you see what
I did there, is I just put pi equals math.pi,
because math.pi, it's a constant, and then
to show you what happens when you print pi,
the variable that I just made, it gives you
the same thing. And that's really nice if
you are going to be typing - going to be using
pi a lot, you don't have to type math.pi,
math.pi, math.pi, you can just type pi. So
that could be handy depending on what you're
doing.
Now what kind of a value is math.pi? Well,
it's a float value. So to see that, let's
just print out the type - so you have the
type, you're asking the computer what's the
type of math.pi, and then you're printing
whatever it comes back with. And you see,
it is a float value.
Let's do an example to see this in action.
You might remember that the circumference
of a circle equals pi times the diameter.
In my math class the teacher actually pointed
to a guy named Craig, and said you can remember
the circumference through "Craig has to go
pi d" and that's a pretty mean thing to do
to some seventh grader named Craig, but I
do still remember it that way in fairness
to her, I remember it as "Craig has to go
pi d," or c equals pi times diameter. Anyway,
let's set up a little formula here, we'll
need a diameter, and let's just make it three.
And we'll need pi. Let's make it math.pi.
And then we'll have c equals math.pi [sic].
And then we'll print c, well what are we doing
here, well we're importing math, so we're
telling the computer, "hey we want to use
the math module," and then we're setting a
variable called d to be the value 3, then
we're setting a variable called pi to be a
value, the float value we just saw math.pi,
and then [laughing], circumference equals
pi times diamter, c equals pi times d.
So then, what we're going to do, is we're
going to print c, it's going to do all the
calculations, store the value - store this
value times that value in the value c, and
then print c. So it's going to print the circumference
of our circle that has a diameter of 3, and
it does. And so as you'll see, our circle
has a circumference of 9.4 and change.
Ok, well, that's just one quick example, you
can imagine any number of things you can do
with this, pretty much anywhere that you want
to plug in pi, you could either set a variable
called pi to math dot pi, or just type math
dot pi.
The next one that I want to discuss is called
tau. Tau - t-a-u, and what in the world is
Tau? Well, it is a constant that has the value
of two times pi. So, tau equals two times
pi. There's a big contingent of people that
prefer to use tau over pi, the practical argument
generally goes that a lot of equations call
for two times pi, so why not make two times
pi its own constant instead of having to write
two pi all the time, if you're interested
in the more theoretical argument that has
to do with radians, there's a lot of literature
out there but this video isn't really about
[laughing] whether you should be using tau
or whether you should be using pi because
the good news is that the Python math module
offers you both. So it's entirely up to you
which one you want to use. The other good
news is that if you know how to use pi in
Python, in the math module that is, then you
know how to use tau in the Python math module
because it's used in the same exact way. We
have math imported, and we type math to show
that we're using the math module, then we
type dot, to show that we're going to use
a function within that math module, and then
tau, t-a-u, and then we have it. So let's
print it, let's see what we come up with.
Print math dot tau, let's run that. And there
it is, 6.28 and change, and in fact tau day
is June 28th and on tau day you can eat twice
as much pie. So just like pi, tau is a number
that can go on and on forever, it's literally
just pi doubled. Python math module gives
you fifteen digits after the decimal, just
like with pi. Now, is tau in the math module
truly two times pi, well, let's take a look.
Let's make a variable, and we'll call it double
pi. And let's set that equal to two times
what we know to be math dot pi, and let's
make another variable and call it tau, and
we'll just set it to math dot tau. So right
now we have double pi, which is two times
math dot pi, and we have tau, which is math
dot tau, there's a couple of things we can
try to do to try to test this out, one is
we can just print both of these, so let's
print double pi, and let's print tau, and
let's run it and see if they come back with
the same thing - and they do. Another thing
we can do is we can make a little if statement,
let's do if double pi is equal to tau - ok,
then what do you want to happen if double
pi is equal to tau? Let's have it print "YIPPEE!
Tau is twice pi!" Ok, but if it's not double
pi, so we'll do that with else, then print
"You're wrong! Tau is definitely not twice
pi!" And hopefully we'll get a yippee out
of here, let's see what happens. Yippee, tau
is twice pi. So for good measure let's go
ahead and put tau in action as well. You might
remember that we just discussed how the circumference
of a circle is pi times the diameter, and
that's how I learned it, "Craig has to go
pi d," well what's the diameter, it's two
times the radius, some of you might have learned
that circumference is two pi r, that is, c
equals 2 times pi times the radius. Well we
don't need to say two times pi, because we
know that it's actually tau times the radius,
because two times pi is the same thing as
tau, so let's make a formula, we can say that
c equals math dot tau times radius, ok well
that's wonderful but we'll need a value for
the radius, and let's use the same circle
that we used before that had a diameter of
3, now let's say it has a radius of 1.5. So
let's - let's - let's try it, we have radius
is one point five, circumference is math dot
tau, so it's tau, times the radius, and let's
print the circumference. What's this doing?
This is telling us, we're going to use the
math module, we have a radius of one point
five, we have circumference which is tau as
it's used in the math module times the radius,
ok, so it stores the value of our circumference
in this variable c, print out that variable
c. What does it say?
Well, it says our circumference, which is
the same value as we had before, for the circle
with a diameter [coughing], excuse me, of
three. Now it has radius of 1.5, same circle.
The - another constant that the math module
gives you is e. e is Euler's number, it's
an irrational like pi or tau, it goes on and
on forever, it doesn't repeat digits, and
just like tau and pi you do not want your
computer to be calculating e forever, that
would really not let you do anything else,
it would just continue to calculate e, so
just like with pi, just like with tau, the
math module gives you an estimation. Let's
see what happens. Now, as before, you have
your math module imported, and the e, it's
math dot e. Pretty similar to the pi and the
tau. And let's go ahead and print that. So
if we print math dot e, what does it give
us? It gives us e, which is 2.71 and change.
Or as before you can always make it into a
variable of course, depending on what you're
doing, you can make e equals math dot e. And
then just print e. And it gives you the same
thing. And it gives it to you as with tau
and as with pi, it gives it to you within
fifteen digits, e is pretty useful, there's
a lot of different times it comes up, a classic
example where e is used in math would be,
if you take a new deck of cards, it's unshuffled,
it's a brand new deck of cards, Ace, Two,
Three, Four of Spades, etc. it goes on and
on, it's completely unshuffled. Then you shuffle
the deck of cards and shuffle it well, [laughing]
I know that sometimes you need to shuffle
a few times, but if you shuffle the deck of
cards properly, it's been proven that the
probability that none of the cards are going
to be back in their original slot that they
were the first time is one over e. And so
that's just one of the many many equations
where e pops up, it's all over math, but just
for purposes of showing math dot e in action,
let's use that as our example. So what we
have, is we have the odds of the cards not
going back in the same place after they're
shuffled, equals one over e. So let's see,
so what we'll do is why don't we have a variable
called odds, and we'll make that one over
e. And instead of - and we have, as you recall,
we already set a variable e equals math dot
e, you can also just do one over math dot
e, which might be easier to follow, and then
let's print the odds. And so that's .36 and
change, so in other words, about 36 point
7879 etc. percent, if you want to make that
more of a percentage you can always multiply
it by a hundred, and now you'll see you're
getting it as 36.7 etc. percent. So there
you have it, the odds that if you take a fresh
deck of cards and shuffle it no card is going
to be back in the same place using math dot
e from the Python math module.
Next up, we're going to discuss infinity.
The math module comes with a means to represent
infinity as a float value. Well, it's not
actually calculating infinity, that would
be impossible, but if you want to represent
infinity in math you can do the following,
you do math for the math module, dot to say
you want to do a function in the math module,
and then i-n-f, inf, short for infinity. And
so let's see what happens when we print that.
Ok, so it prints you inf, i-n-f. Well you're
thinking, "hmmph, 'inf,' big deal - I could
just, you know, I could just do this, I could
print inf, and hey it's the same thing, I
got inf." Well, you're on the right track
in some ways, but the infinity in the math
module is not the same as just the string
inf, and to show that we can check the type,
and let's print type math dot inf and what
we will see is that it's actually a float
value, and not a string versus if we were
just doing the previous example where where
it's i-n-f, well that is, that is a string.
And so it's a different type of value, but
maybe it's not so different, let's see. One
thing we can do, using the math module, is
we can check whether something or not is infinite,
so let's do that. That is math dot isinf and
let's just do this with infinity - so what
is this doing, this is math for the math module,
dot to show "hey, I'm going to use the math
module for a function," isinf is our function,
this is asking is it infinity, and then math.inf
is as you'll recall the float value that gives
you i-n-f. Well is this an infinite value?
Uh, well of course that didn't do anything
because we'll need to print it. Is this an
infinite value? Yes, true. Ok, but then what
happens if we just do our friend the string
i-n-f? Well, this will be interesting, it's
an error. And that's because it says "hey,
this must be a real number, not a string!
You fool!" Ok, well [laughing], well, ok,
fine, say "ok fine, but I'm going to convert
it to a float." So why don't I do, let's make
our string inf into a float, by making it
float inf - float inf, and it's true! It's
true?? If you convert the float of just the
word "inf" if you convert that string into
a float and then you say "hey, is that infinite"
and then it prints back True, that is infinite??
Well, what's going on here? math dot inf is
actually the same thing as the float of inf.
Well, don't take my word for it, let's see
if we can prove it in a couple of ways. Let's
try making another if/then statement. So if
math dot inf is equal to float of inf then
let's print "Holy moly, they are the same!"
Let's get rid of this extra f. Else let's
print "Nope! Not the same. Frowny face." Ok,
so let's see what happens. Ahh, holy moly,
they are the same. Let's clear the console
and try again. Holy moly they are the same.
Ok, so while we're on the subject of infinity,
you also can do negative infinity, and you
just do that by doing negative math dot inf.
So let's start again, as you see it's negative
math dot inf. And that gives you negative
infinity, so let's try printing that. Print
negative math dot inf - let's clear this out
- and there it is, negative inf. And is that
the same, you may be wondering, as saying
float negative i-n-f? Because you see it's
negative i-n-f, is that the same as saying
float negative infinity like this? Well, let's
find out, if float of negative infinity is
equal to float - oh no [laughing], is float
negative infinity going to be equal to float
negative infinity [laughing] - is float negative
infinity going to be equal to negative math
dot inf? Well, if float of negative inf equals
negative math dot inf, print "Hooray! They
are equal!" And it's important to always be
grammatically correct when saying "Hooray!
They are Equal!" Ok, else print "Those two
aren't equal! What are you trying to pull?"
Ok, let's find out what happens. Hooray! They're
equal! Ok great. And does the math module
consider this to be an infinite value? Well
let's try the isinf function. So let's print
math - see it lights up, math dot isinf and
let's try negative math dot inf, negative
infinity, and let's see if it's true - and
it is, it is true, and so negative infinity
is also considered to be an infinite value,
so you could do positive or negative infinity.
Well, what are some things we can do with
infinity, let's open up the console and let's
play around with this a little bit. And I'll
make this a little bigger.
So, we'll import math. And then let's do math
dot infinity plus four, and what do we think
is going to happen when we add four to infinity?
It's still infinity. Infinity plus four is
just infinity. Ok, well, what about math dot
infinity times three? And that is still infinity.
You multiply infinity by three times, that's
infinity. Well, let's try a more tricky one,
what if we subtract infinity from infinity?
math dot infinity minus math dot infinity.
You might think "oh, zero, right? x minus
x equals zero, math dot infinity minus math
dot infinity, that should be zero," well,
let's find out. n-a-n, it is "not a number."
Well infinity, it stretches out forever, perhaps
you can't really subtract infinity from infinity,
and it gives you this interesting value nan,
whcih stands for not a number what in the
world does that mean? Well the good news is
we're going to be talking about it right now.
Sometimes you might want a float value that's
not a number. And well, maybe you think "hmm,
well I just did the float of i-n-f, maybe
we can do that with just anything?" So you
know, you saw we just did print float i-n-f,
well, and that printed something, so what
if I did print float of hi? Is that going
to give me a float with the value of hi? No,
it gives you an error and says ValueError:
could not convert string to float: "Hi" - so
you can't do this with everything, there's
actually only, only a couple of things where
you can type in letters like that and convert
it to a float, and one thing you can do, is
you can do a float of nan, and that is not
a number. And n-a-n, and it gives you n-a-n,
and that's nan as a float value, and just
like infinity. But you could also use our
math module that we have imported here and
aren't even using, so how do we do that? Well,
it's math dot nan and that gives you the float
value of not a number as well. So, just to
show you that in action, print math dot nan,
and it gives you not a number. And so it gives
you that float value nan, not a number. So
let's check it's type, print the type off
math dot nan [pronounced n-a-n] or nan [rhymes
with man] people call it different things,
and it is a float class, and is float nan
equal to math dot nan, - let's find out. If
float nan - 'cause remember, we did this with
infinity where float of infinity was the same
as math dot infinity, is it, is it going to
be the same for not a number? Let's find out
if the float of not a number is the same as
the math dot not a number. Let's find out.
So let's do, if float of not a number is equal
to math dot not a number print "Yes! These
two are equal values! Hooray! Hooray!" else
print "Nope! Those are not equal values! Not
at all!" Well, what do you think is going
to happen? You might think because it worked
for inf, maybe it will work for nan. Nope!
Those are not equal values, not at all. Well
why in the world not? Well [laughing] the
answer interestingly enough is that nan does
not equal itself! It's not a number, it doesn't
equal anything, it doesn't even equal itself.
So if I did this same thing, just with math
dot nan, these are literally two things, if
math dot nan is equal to math dot nan print,
y'know, yes or no. No! It's not an equal value.
So, I will represent to you [laughing] that
float of nan is - that math dot nan and float
nan are different means of expressing the
same value, but they're not going to show
up as being equal to each other because nan
does not equal nan to begin with.
All right, let's - let's - let's - let's see
this in action. Let's say that we have a variable
called math dot nan times six. So the variable
is just called v-a-r, and it equals not a
number times six. And when we print that out,
what do you think is going to happen when
we multiply something that's not a number
times six? It is itself not a number. It's
not a number either. Not a number times six
is also just not a number.
Well, does that mean any time you ever throw
nan in the mix it will always return with
not a number? Well, let's see, let's change
our formula here and try not a number - math
dot nan - times zero. Well, as you probably
know, anything times zero is going to be zero,
so even if it's not a number, I mean, if you
multiply it zero times, maybe it will be zero.
Let's see. No. It's not a number either. You've
probably heard that anything to the power
of zero gives you one, well what if we do
math dot nan, and then when you do to the
power of that's actually two asterisks, star
star, to the power of zero. So, math dot nan
to the power of zero, what's this going to
give you? Now you're probably sitting there
thinking, "oh, I know, it's going to give
me not a number, if nan times zero is not
a number, then raised to the power of zero
- any time you throw it in, it's going to
be not a number, right?" Well, let's see - no,
it gives you one. And one more example, one
to the power of math.nan - one to the power
of not a number - it gives you one. Why do
these two exponents work [laughing] why do
these two exponents work when multiplying
nan by zero doesn't? I didn't know, I did
some Googling and from what I can tell it's
just a byproduct of historical conventions
in computing that were carried over into Python,
but if you have a better explanation leave
a comment and let us all know. As far as I
can tell there's no particular reason aside
from that's how it's always been.
So last thing, and there's one more issue
with nan, and this one it's probably easiest
just to show you from the console, so let's
do that, let's actually just make a new console
I know I have a lot of consoles up, so let's
clear these out. Ok, and so here's an issue
with nan. You might be able to check to see
if something is true or not in math just with
the console here, so say you wanted to know
"hey, four equals two plus two - so four is
equal to two plus two, is that a true statement?"
You hit enter and if it's a true statement
it will say true, if it's a false statement
it will say false, something you can do with
the console. Ok, well it's true, four does
equal two plus two, that's true. Ok, what
about four is equal to two plus three? Hmm,
no that's false, ok, sure, we're on the same
page here, four is equal to two plus two,
four is not equal to two plus three, that's
false. Ok, well, let's import math into the
console, and let's do math dot nan is equal
to math dot nan - and do you have a guess
as to whether this is going to show up as
true or false? It's false, because not a number
doesn't equal anything, not even [laughing],
not even another not a number. And so those
- those don't equal each other, it's false
- and that can present some issues because
what if we were curious about if math dot
nan is equal to math dot nan times six? Well,
what's this asking, you might think this is
asking - is math dot nan times six is also
not a number? That would be a reasonable way
to look at this expression and think that's
what it's asking. But it presents you with
false, when we just saw a few minutes ago
that when you do an operation with not a number
it is not a number, so it should be returning
not a number. So what's going on here, well,
it's saying this value that does not equal
this value, times six, it doesn't equal, it's
just not on the same side, so how do you check
whether a figure is not a number or not if
you can't do that? Well what you can do, is
there's a different function in math called
math dot isnan and that asks the question,
"hey is this a nan value or isn't it?" And
there when we do math dot nan times six which
is the same thing we did up here, that time
it's true. And let's compare that to the other
value we just saw where where we did - math.isnan
math.nan to the power of zero which remember
gives you a numerical value that's not a not
a number value, so math.isnan - this is asking
the question "hey, is this a nan value?" False,
no, it's not a nan value, that gives you a
regular value.
And so that should conclude the video for
today, these are the different constants that
you can use in the math module in Python.
Thanks very much for watching.
