From the course: Python Essential Training

Ints and floats - Python Tutorial

From the course: Python Essential Training

Ints and floats

- [Instructor] Okay, let's go back to ints and floats, two basic Python number types, and let's look at what happens when you use them together, convert between the two, and how to watch out for some of the common pitfalls of each. So earlier we saw an example of doing math with ints and getting a float back. So, 20 is an integer, four as an integer, but we get back this float, 5.0, and Python is trying to be helpful here and always return a float from division, because there's a potential for non-whole numbers to come back. Similarly, if we add a float with an int, we get back a float, and the same goes for multiplication and also exponents. 256. And we want to convert this back to an int. We can use the int class to do that. So if I just grab this 256 as an integer. Now notice I said the int class, not function, and it looks like a function, it's lowercase, but int is actually a built-in class in Python. And Python sort of breaks these capitalization rules here, specifically for its built-in classes. So for, actually all of these things, strings, ints, floats, list, these are all classes. So when you convert from one type to another type, like here, I'm converting from a float to an int, this is called what programmer's call casting. So I'm casting from the float 256 to the integer 256. So, casting this is pretty straightforward, but what about let's say 8.9? Hmm, that's an eight. What about 8.99999 and then some other nines. Very, very close to nine, but it's still the integer eight. So when you cast from a float to an int, Python does not round for you. All it does is lops off everything after the decimal place. Ints don't have decimal places, it just throws that part away. So you have to be really careful. Say, if you're doing something like this, which is very close to five, it will actually round to, not round, but turn it into four. If we want to round this to the nearest integer, we can use the round function and that gets you back five. Also, as an argument, we can pass in the number of decimal places we want to round it to. So, 4.67 and that's actually doing rounding for you. So, let's look at one of the pitfalls of floats. Let's do 1.2 minus 1.0. So I'm a programmer, not a mathematician, but that doesn't look quite right to me. So what's going on here? If you look back at 14 over 3, you'll see that this is represented with a 67 at the end. So this, what 14 over 3 actually is, is 4.6 repeating, but its float representation literally ends in a 67. Floats are approximations. Floats are stored as binary ones and zeros in memory, and there's only a finite amount of memory to store them, so Python uses some tricks and approximations and this can occasionally result in weird rounding errors that result in this kind of thing. Now, if you use the round function on something like this, you should be fine and we should probably give that a couple decimal places, but you should be a little cautious about using floats for this reason. Next, we're going to look at some other ways of dealing with decimals, particularly in situations where the number of decimal places is known, like with money.

Contents