Loading video player…

Floats

00:00 Floating-Point Numbers.

00:06 Now, what is a floating-point number? Well, a floating-point number is any number with a decimal point. These are represented differently in Python’s memory than the integers we’ve already seen. And while you’ve seen some maths in the previous section, any division operation will return a float, as you will see in a little more detail later on,

00:30 and they can be defined using scientific notation, such as seen onscreen here with 4 * 10 to the power of 3, which is equal to 4,000. But there are some limitations to floating-point numbers, as you will see at the end of this section, and this is something you will fall foul of at some point and need to be aware of.

00:51 The simplest way to define a floating-point number in Python is to create a variable with a decimal point and a number after it, such as seen here, where we have a = 4.2.

01:03 You can see that the value is 4.2, and entering type(a) shows a <class 'float'> and a floating-point number has been created.

01:13 A number which has got a decimal after the point, even if it’s 0, will still be a float. So while that could be represented as an int, it is a float. Coming up, you’ll see that the result of division will always be a float.

01:29 We’re going to define some variables to work with to make the division clear to see. We’ll have c = 10 and then d = 4. Both of those are ints, but if we make e = c / d,

01:49 we can see the result, which is e, which is a float. The result of division will always be a float, even if the answer could be an int.

02:02 So, obviously, in that case, 10 divided by 4 is 2.5, so the answer would need to be a floating-point number, but if d is redefined to be 5, and we make f = c / d again,

02:19 we can see that f is a float even though the answer could be an int. Python does have the integer division operator, which is the double divide sign (//), as seen here, and we can see the result is 2.

02:32 But there is another thing to be aware of with this—it will always give you an integer result, which may not be the exact answer you’re looking for. So here, if we take c and divide it by 4, the answer should be 2.5 but we can see it’s also 2.

02:50 This is something you need to be aware of when choosing which division operator you’re going to make use of. Sometimes floor division gives you the right number, and other times floor division gives you the wrong number.

03:06 It’s possible to take any number, including an int and turn it into a float using the float() keyword.

03:17 This can be seen here by making i = float(3), and then if we look at i, we can see i has the value 3.0.

03:28 Now, something else which is useful is being able to define numbers using scientific notation. If you’re not aware of what scientific notation is, a little bit of research on Wikipedia will help you understand it, but the general form is that there will be a number at the beginning and then the second part will be the powers of 10 which are being applied to that number. So here, we’re going to define the number j as 4, and then use an e, to denote scientific notation, and a 3.

03:58 And you can see that equals 4000.0 because we have a 4 at the beginning, and then we have 3 powers of 10, which it’s multiplied by, so it’s 4 * 1000.

04:10 The powers can also be negative, as seen here. 4e-3 equates to 0.004, because 10 to the -3 is one thousandth, and then we have four of those, which is why we have that 4 there.

04:28 Sometimes the negative form is a little less intuitive, but with a bit of practice, it will make sense. Floating-point errors were mentioned at the beginning of this section, and this isn’t something that only affects Python, but all computer languages. Here’s an example of floating-point arithmetic not working in the way you would think.

04:46 It’s pretty easy to encounter these kinds of errors. Let’s just define some simple variables. So here you can see a = 0.2 and b = 0.1 and c is going to be a + b.

04:59 Now, I’m sure you’re all aware of what the answer should be, 0.3, but if we look at c, we can see there’s a tiny error at the end.

05:09 This is something that you will need to be aware of, and you’ll need to take it into account when programming. Now, that’s obviously something that’s outside the scope of this introduction video, but it is something you’ll need to be aware of. The first time you encounter it in the wild you’ll probably scratch your head for a while, but after a while, you’ll know what it is and know what to look for.

Avatar image for Zarata

Zarata on March 30, 2020

I’m appreciating your skill as a teacher in particular, and the quality and ‘excellent’ attitude of Real Python as a whole. You all have made a monster, though: all of the encouragement encourages me to participate and post this point! I’m a long-time programmer now wishing to pick up Python, and I’m a personality type which tends to take some point and run to try variations “before their time” – perhaps before allowing time for explanation of other details in their good order. I guess I didn’t learn … when they said “brains”, I thought they said “trains”, so I ran to catch one ....

At any rate, I’ve noted by midway this section, or a bit further, that you’ve referred to ‘//’ both as an “integer divide” and as a “floor divide”. You’ve also used the words – in a context –

it [the result of ‘//’] will always be an integer result which may not be what you are looking for.

So, I ran off in a direction. As I meandered, I discovered the ‘//’ result (at least in v3.8.2) is not always an integer. The dynamically typed result of ‘//’ depends upon the argument types with float dominating:

>>>c=10
>>>d=3
>>>e=10.0
>>>f=3.0
# int // int
>>>c//d
3
# float // float
>>>e//f
3.0
# int // float
>>>c//f
3.0
# float // int
>>>e//d
3.0

’//’ in v3.8.2 is apparently always “floor divide”. The integer result contrast of ‘//’ to the always float result of ‘/’ exists only for the special case ‘int // int’.

Perhaps this is a v.3.8 change from v3.6???

Should I post again, I’ll be briefer. :) Thanks for your instruction!

Become a Member to join the conversation.