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.
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
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
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
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.
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
4, and then use an
e, to denote scientific notation, and a
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.
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.
Become a Member to join the conversation.