Floating-Point Numbers
00:00 In this lesson, you’ll learn how to represent numbers using Python’s floating-point data type, which is most suitable for numbers with a fractional part.
00:10 As with integers, you can create floating-point numbers using their literal form. The only difference between an integer literal and a floating-point one is that a floating-point literal must have a decimal point to separate the whole part from the fractional part.
00:24
You don’t even need the trailing zero to define a floating-point number as long as you include the decimal point in your literal. Similarly, you can skip the leading zero if your number is a fraction, like .42
.
00:39
Floating-point numbers in Python support the same grouping of digits as integers with the help of the _
(underscore) character.
00:47 As you can see, it’s possible to express the same value, such as 42, using different data types in Python. The difference is how they represent it in your computer’s memory and how much space they take, which in turn affects the performance and precision of various operations.
01:05 In addition to this, you can define floating-point literals in Python using the scientific notation, which is sometimes known as the exponential notation or the E notation.
01:15
The idea behind it is to collapse the repeated zeros in a really big or a really small number and only focus on its significant digits. For example, you can write the same number as 4.2
times 10 to the power of 7 (4.2e7
). The letter e
, which can be either lowercase or uppercase, stands for the exponent.
01:37 The exponent itself must be an integer, but may be negative, just as the number in front of it.
01:45 Such a notation may appear strange or inconvenient, but it can sometimes make handling big or small numbers easier in arithmetic operations. You might have seen the E notation on handheld calculators, which use it to represent numbers that wouldn’t be able to fit on the screen otherwise. If you don’t like this notation, then you can format your floating-point number using a fixed number of decimal places, such as eight, to reveal all zeros.
02:14
Like with integers, you can create floating-point numbers into ways. You can either use their literals consisting of digits and the decimal point, or you can call the built-in float()
function.
02:25 When you call the function without any arguments, it returns a floating-point number equal to zero. Note that this is different from an integer zero. However, in most cases, the function will accept a single argument, which can be either a string or a number, such as an integer,
02:48
That’s convenient for converting other data types to floating-point numbers. You’ll also want to use the float()
function to define a few special values which don’t have literal representations in the floating-point number data type.
03:02
In those cases, you’ll pass a particular string value as an argument to the float()
function. Two of them are positive and negative infinity ("inf"
).
03:12 These are useful as unbounded upper and lower bounds when you search for a maximum or minimum value. For example, conveniently, you can compare infinity to regular numbers, which gives sensible results.
03:25
The third special value in the floating-point data type is called Not a Number ("nan"
). You can use it to explicitly indicate a missing value. However, it’s more commonly used by Python itself to signal an undefined mathematical operation, such as dividing infinity by another infinity.
03:44 There’s one interesting quirk about Not a Number. Because Python follows the IEEE 754 specification for the floating-point arithmetic, a Not a Number is never equal to any value, not even itself.
03:59 Another thing that’s different about floating-point numbers when you compare them to integers is that they have a fixed limit. If you try defining a value bigger than the allowed maximum value, then you’ll end up with infinity. How do you know what’s the maximum value?
04:14 Well, in practice, it doesn’t really matter because the range of floating-point numbers is so enormous that it will be more than enough in most cases Though if you really want to find out, then you can use the following instruction.
04:30 In this case, the maximum value is about 1.8 times 10 to the power of 308. Just remember that this number isn’t set in stone because various Python interpreters can have different limits depending on the platform they were built for. To sum up, you can create floating-point numbers in Python using their literals, that must contain the decimal point. At the same time, you don’t need the trailing or leading zeros in those literals, leaving only the decimal point in place.
05:00 You can specify floating point literals using the scientific notation known as the E notation, which lets you express really big or really small floating-point numbers in a concise way.
05:11
The second way of defining floating-point numbers and Python takes advantage of the built-in float()
function. When you call the function with no arguments, it’ll return a floating-point number equal to zero. However, you are almost always going to call it with one argument to convert another data type into a floating-point number. Specifically, you can convert a string composed of decimal digits and a few special characters, such as the -
(minus sign), or you can convert an integer or even another floating-point number.
05:42
There are also a few special floating-point values that you can obtain with the help of the float()
function. They are the -∞
(negative infinity), ∞
(positive infinity), and Not a Number.
05:54 Okay, now that you know about integers and floating-point numbers, it’s time to learn how you can use them in arithmetic expressions in Python.
Become a Member to join the conversation.