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.
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
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.
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: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: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,
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: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.
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.
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.
Become a Member to join the conversation.