Locked lesson.
About this lesson
What's the difference between a floating point number and an integer?
Exercise files
Download this lesson’s related exercise files.
Numbers.docx60.1 KB Numbers - Solution.docx
58.9 KB
Quick reference
Numbers
Python breaks numbers down into different types, including floats and ints.
When to use
Whenever you need to use numbers, keep the different types in mind.
Instructions
Python has a couple of different number data types:
floats = decimal numbers
ints = whole integers
Hints & tips
- Use floats when you need exact numbers
- Use ints when you need whole numbers
- 00:04 In this video, we're going to look at number data types.
- 00:06 And I mentioned earlier when we talked about the different data types that
- 00:10 numbers are a data type.
- 00:11 And that's not precisely true, a number is a number, but
- 00:15 numbers in Python get broken up into different things.
- 00:18 There are floats, there are integers, there are several others as well.
- 00:21 We're just going to look at floats and integers in this video.
- 00:24 And in fact, in this course, these are the two main ones.
- 00:27 So let's create a variable, we'll call it num_1, and set it equal to 10.
- 00:31 And let's do another one called num_2, and set it equal to 3.
- 00:36 Now we learned in the last video all about math,
- 00:41 so we can go print (num_1 / num_2).
- 00:46 And if we save this and run it, we get 3.
- 00:48 Well, that's not really true, is it?
- 00:50 If I pull up my handy-dandy calculator here and if I go 10 divided by 3,
- 00:55 I see the answer is 3.3333333.
- 00:58 Python is trying to tell us that the answer is 3.
- 01:00 So what's going on here?
- 01:02 Well, we're using whole numbers or integers, right?
- 01:05 And an integer can be positive or negative, it could be -10,
- 01:08 that's still an integer, but they're whole numbers.
- 01:11 And when you use whole numbers like this, Python treats them like whole numbers and
- 01:15 it round up or rounds down.
- 01:17 So that's why we're getting the answer 3, because Python says, okay, you want to
- 01:21 use integers, I'll return an integer and an integer is a whole number 3.
- 01:25 Now, sometimes that's what you want, sometimes you want precision,
- 01:28 you want the actual decimal point answers.
- 01:30 Especially if you're building a shopping cart,
- 01:32 somebody's buying something from you, it's $19.95.
- 01:35 You don't want to just round it up to 20,
- 01:37 you want to be able to handle those decimal points.
- 01:39 And for that, we use something called floats.
- 01:42 They're floating point numbers, and it just means decimal, basically.
- 01:45 So if we go 10.0 divided by 3.0,
- 01:49 now Python goes okay, you're using floats, I can return a more precise answer.
- 01:54 So if we save this, run it, we get 3.33333,
- 01:57 which is pretty much what our nice handy dandy calculator showed us.
- 02:02 So it's important that you pick which one you need to use.
- 02:06 So if you need decimals, you're definitely going to want to use decimals
- 02:09 in your coding, right, because Python can't guess this stuff.
- 02:12 You have to be very specific.
- 02:13 You have to tell it otherwise, if you're just using whole numbers, these integers,
- 02:18 it's going to return an integer answer.
- 02:20 And sometimes that's fine, sometimes you don't want that.
- 02:23 So one kind of interesting thing, if we use a float, 10.0, and
- 02:27 right here I'm just going to write decimals.
- 02:30 Here I'm going to put whole numbers, int, it stands for integers.
- 02:36 So anyway, 10.0 and 3, what do you think's going to happen here?
- 02:40 If you run this, again we get 3.333.
- 02:43 You only have to use the float for one of these things, and
- 02:47 then Python figures out okay, they want to use floats.
- 02:51 So we could switch it around even, still going to get 3.333.
- 02:55 That's kind of interesting.
- 02:56 So those are the number data types, pretty straightforward.
- 02:59 I've mentioned in an earlier video, when you're dealing with variables and numbers,
- 03:03 you don't ever want to wrap your numbers in quotation marks,
- 03:07 because this is no longer a number.
- 03:08 Now this is a string, because anything wrapped in quotation marks is a string.
- 03:12 And if we try and save this and run it,
- 03:14 we're going to get an error because you can see, it's string and integer.
- 03:18 You can't take the word 10 divided by the number 3,
- 03:22 it just confuses the heck out of Python.
- 03:24 So be sure and be consistent and never use quotation marks whenever you're
- 03:29 dealing with numbers, unless you want it to be, if you have a string and
- 03:33 it's like, my favorite number is 10.
- 03:36 And obviously you can use a number inside here, but
- 03:39 you can't do math with this obviously.
- 03:41 So in the next video, we're going to look at assignment operators.
Lesson notes are only available for subscribers.