Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There're basically two different kinds of numbers in Python. Let's go ahead and make a working copy of variables.py, and we'll look at them, variables-numbers.py. We'll open our working copy. And we'll go ahead and define a variable. And we'll give it a value. And then we'll print that variable and save and run.
And you see we get that value.
So what we have here is an integer type.
So if I type num and I'll also have num, so we'll get both the type and the value,
we'll go ahead and run that,
and we see that its class is int, and its value is 42.
If I say 42.0, now its type is going to be
You have integers, and you have floating point numbers. If I take an integer like this, and I say 42/ 9, I'm no longer going to have an integer. If I save this and run it, we'll see that I have class 'float' and that it is this real number with a whole lot of digits after the decimal point. If I want to get an integer division, I can use two slashes instead of one. And now what I'll get, instead of 4.66666, I will get just 4.
You'll notice that it is not rounded up. So if I want it to be rounded, I can type round and get back the floating point division and save that and run it, and now we have it rounded up. It's a 5. I can give another argument to round it to how many digits I want it to round to. I can save that and run it. And now we have 4.67. In addition to the integer division with the two slashes, remember it was like this - if I save that and run it, we get the 4 - I might also want the remainder, which is also called modulo, and that's with the % sign.
So if I save that and run it, I have 6. That's the remainder after you do that 42/9. It's 4 with a remainder of 6. If I have a floating point number, say 42.9, but I want to make sure that it's actually going to be read as an integer, I can say int of, like this, and save that and run it, and I get just the 42 part.
Likewise, if I have an integer number and I want the floating point representation of it, I can say float like that and save that and run it. And I actually get a floating point representation of the integer 42. Float and int are actually object constructors. These are constructors for those classes. So when I say float 42, what I'm actually doing is I'm creating an object of type float, and I'm giving it an initial value of 42.
That's getting passed to the constructor. So those are the basic ways that you can use and create integers and floating point numbers in Python 3.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101260 Viewers
61 Video lessons · 88019 Viewers
71 Video lessons · 71883 Viewers
56 Video lessons · 103720 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.