Define Floating-Point Literals (Solution)
00:00
As before, I have IDLE’s interactive shell open on the left and a placeholder file for the exercise solution on the right. It already has the instructions defined as a multiline Python string at the top. Once again, we’re going to define a variable, so we start typing the name of the variable, which is num
, followed by the assignment operator (=
) and the floating-point literal.
00:24 The value of that variable should be 175 thousand, but if I write it like this, Python would create an integer instead of a floating-point number. To fix that, I can just append the decimal point and a zero. This is great.
00:39
We’ve assigned the expected numeric value to the variable, but they’re specifically asking us to use E notation in the floating-point literal. With E notation, Python takes the number to the left of the e
and multiplies it by 10 raised to the power of the number after the e
. For example, 0.001 times 10 to the power of 3, or 1000, will move the decimal point 3 places to the right.
01:08
Now it’s up to you how many significant digits you want to keep in front of the decimal point and what exponent to use. You could, for example, write 175e3
to arrive at the same value.
01:23
Another option would be to write something like 1.75
and adjust the number of decimal places accordingly.
01:31 That’s E notation in a nutshell. Let’s assign this literal to our variable
01:39 and, lastly, print the variable onto the screen.
01:43 Remember to always verify if your program works as expected by saving it and running it through Python. Great. It seems that our program is correct, which concludes this exercise.
Become a Member to join the conversation.