In this video, Kathryn shows you how to import the math module, and as how to use it to calculate cosine and sine in Python, as well as ceiling and floor. Then, she demonstrates some of the constants available in this module, such as pi and e.
- [Narrator] Modules are built into Python, but we must import each module we want to use. By built in, we mean that we don't have to download anything extra from the Internet to use it. And so, to import the math module here, we'll go import math. Now, to use the math module, we just have to go math dot and we get access to all of these methods. Depending on what IDE you're using, you might not get all of these shown up in your editor like I do, but they all still exist, and you can look them up in the documentation. In this video, we'll be going over some of the most important ones.
And to start off, we'll talk about the constants that are built in to the math module. One constant is pi, so you can just do math dot pi and you have access to pi. And so we'll just go ahead and print it out here so we can see it in our console. And here it prints out pi. Another constant we can print out is e. And so we can go print math dot e accessing that constant, and there it is again printed in the console. Now, there are some new constants built into Python 3.5.
And so we have print math dot N-A-N, which stands for not a number, so this is a value you could give something in your program that's not a number. We also have infinity, so math dot infinity and then we'll have negative infinity, which is just negative math dot infinity. Printing these out, we get not a number, infinity, negative infinity. We can also do trigonometry with the math module. Just to review, cosine measures the adjacent side over the hypotenuse and sine measures the opposite side over the hypotenuse.
This means the x portion here is like the sine value and the y portion is like the cosine value. At a 45 degree angle, these are mathematically the same. In the code, we can also prove that they have the same value. So say we have a variable called obstacle direction here. And we're going to have it be the value math dot cosine, math dot pi over four, which is that 45 degree angle cosine value. We can go ahead and print out the obstacle direction here and there we go, we have the cosine value.
Now to double check that this value is the same as the sine, we'll go print math dot sine, math dot pi divided by four and this should be the same value and that's what we get in the console. Now let's move onto the ceiling and floor functions. So let's create a variable called cookies, and we'll have this equal 10.3. And then we'll have another variable called candy and have that equal seven. So imagine the stance for how many cookies and how much candy you have currently in your house.
No one really has 10.3 cookies. What is .3? It's a third, but really it looks like you have 11 cookies because you have 11 things. We can use the ceiling function from the math module to bring this up to 11. And so we can go print, math dot ceil cookies and this will bring cookies back up to 11 because you have 11 things if you have 10.3 cookies. And say we do it on candy. So we'll go print math dot ceil candy and this will return the same value because you don't have a decimal that's going to bump it up to the next integer.
We can also do the opposite of this with the floor function. And so we can have a variable called age and we'll give it the value 47.9. That's our age. But you don't really want to tell people that you're almost 48. You just want to say you're 47. And so we can use the floor function to bring 47.9 back down to 47. And so we'll go print math dot floor age and there we go. We bring it back down to 47, and you're only 47, not 48.
You could also do this if your age was actually just 47. You could have 47, but if you use the floor function on it, the same value will still be returned. And so we'll have print math dot floor and put the other age in there. We'll still get 47 because there's no decimal value that's going to be removed because it's only a whole number. These are only some of the constants and functions built into the math module. But in the next video, we'll get into some of the more complicated methods.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content