Using Scientific Notation and Complex Numbers
00:00
Let’s continue exploring some format specifiers you can use when formatting floats using f-strings. You can launch a new Python in REPL and you can create a variable called speed_of_light
.
00:11
And if you don’t remember, this value off the top of your head, neither do I, but I’ve checked it and it’s 299792458
and that would be meters per second.
00:21 Incidentally, you can’t put the comma as a thousand separator when you’re typing an integer as in this case. However, if you prefer, you can put in an underscore and this serves the same purpose as when you use commas in other places.
00:35 You can’t use a comma here because commas are used to define tuples in Python, but the underscore is a visual representation. It doesn’t change the number.
00:42 The number is exactly the same. It’s just a visual way of displaying it when you are using a literal integer literal in this case. Let’s create an f-string that says the speed of light.
00:53
And this is The speed of light in vacuum
. It changes in other media is
and here we can put the braces and put speed_of_light
.
01:01 And for the time being, we can leave it as it is meters per second.
01:06
And let’s say so you know where the vacuum is and you can see that even though I used underscores when I defined the variable speed_of_light
, the underscores are not displayed.
01:14 Python displays this in the standard way it displays integers of any lengths. You’ve already seen how if you want, you can put in the thousand separator by putting the colon, which shows that we’re going to add format specifiers and the comma.
01:30 You can also, if you want, put an underscore if you want to display it in a Python-specific way. This is how you would define it in Python if you wanted to show a thousand separator.
01:41
We’ve also seen how you could, for example, put .2f
and that shows that you want two digits after the decimal point. But in this case, this doesn’t make much sense because we don’t have any numbers after the decimal point so it simply adds 00
.
01:56
But let’s use a similar notation to this. Dot, let’s put 12
for now, but instead of f
, I’m going to use g
and this specifier is a general format specifier.
02:07 And let’s see how Python f-strings deal with this.
02:10
And so far there’s no change from the standard one. We’re told that the speed of light in vacuum is 299792458
. So what does the .12g
mean?
02:21 So this means we want 12 significant digits in our number.
02:27
Now the speed of light in vacuum 299792458
has nine digits. And since 12 is larger than nine, we can’t have more significant digits. So Python simply shows us the number as it is.
02:39
In fact, we could go all the way down to nine significant digits and we still get the number in full 299792458
meters per second. But, what happens if I try to display the number using only eight significant digits?
02:55
The final digit, which is an eight in the speed_
of_light
value, must be removed now. And how can we do that? And Python switches to the scientific notation.
03:06
So now the speed of light in vacuum is 2.
And then we have the rest of the numbers, except that the last two, which are 58
, are rounded up to 6
.
03:17
Why? Because the eight forces the five to be rounded upwards. And then you have the e
notation, the scientific notation that’s commonly used not just in Python e+08
.
03:27 And if you’re not familiar with this, this means that this number is equivalent to multiplying it by 10 to the power of eight.
03:35
Let me give you an example. This is a literal as well. So if you type in 1e6
, this is the same as one multiplied by 10 to the power of six, and that’s 1 million.
03:46
Another way of thinking of this is that there are six zeros after the one. So there are six places, in this case, zeros after the first value. So 5e6
, which is 5 million has six zeros after the five.
04:01
And if I try to use 3e8
, this is the same as three times 10 to the power of eight, which is three with eight zeros after it. And this is very similar to the speed of light there.
04:13
You can see you have 2.99e+08
. So we get a sense of what the scientific notation looks like.
04:22
Let’s go back to our f-string. We’ve seen what it looks like with eight significant digits. Let’s go down to five. And now we have 2.9979
. The five significant digits in this case are the 2
, 9
, 9
, 7
, and 9
.
04:39 There are five of them. So the number has been rounded to fit and it’s still timestamped eight. That will never change because that’s the value of the speed of light.
04:49
Let’s go down further, go down four, 2.998
.
04:54
When I go down to three significant digits, the first three significant digits are 2.99
, but since the fourth one is eight, this will round upwards and therefore this will round all the way up to 3e+8
.
05:06 So for this number, since the second and third significant digits are nine and nine, anything with three, two, or one significant digits end up being the same number.
05:17
Now, let’s go back to the first experiment we did with this g
format specifier, the one which had 12 digits. And you may recall that 12 is more than the number of digits we have so it shows us the number as it is.
05:31
Sometimes you may want to always use the scientific notation in all instances, even in this case. And in these cases, instead of using the g
format specifier, you can use the e
, the term you may be familiar with from other places, if you’re familiar with scientific notation.
05:48
And now we can see the speed of light in vacuum is 2.99792458
. But then we have a few more zeros. Why? Because now we want 12 digits after the decimal point.
06:00 So that’s why we need to pad with a few more zeros. And if we say we only want nine digits after the decimal point, you can see that one zero has been added,
06:12 eight fits all the nine significant digits, the two which is before the decimal point and then the eight that come after. But anything below that will start to round the number.
06:23
So this is similar to using g
in this case, although note that when you use g
, the number after the dot represents significant digits, whereas in this case it represents how many digits you want after the decimal point, as was the case when you used the f
modifier.
06:39
So when you use e
, you’re ensuring that the number is always displayed using scientific notation.
06:46
And let me finish off this lesson with a brief mention of complex numbers in case this is something you want to use. Let’s create an f-string and let’s put in a complex number here, for example, 3.0 +
something else.
07:01
And then we need to put a j
to make it a complex number. So that shows a complex number. You can use all the same formatting, for example, if you want them to be in scientific notation, or perhaps you want them to be only with two digits after the decimal point using scientific notation that also works.
07:18
And you can revert back to your f
if you just want floats. So luckily Python’s f-strings deal with complex numbers for you so you don’t have to worry too much about how to format them.
07:32
And let’s finish the lesson by summarizing the key points. You can use the g
format specifier, and this stands for general format. In Python’s f-strings, in this case, they pick either the standard fixed-point format, the one you get with the f
format specifier or the scientific format, depending on the number’s magnitude and how many significant digits you require.
07:54
The e
format makes sure the number is always displayed using the scientific notation, the e+
a number, or e-
a number.
08:03 And if you need to work with complex numbers, then all the relevant format specifiers apply to the real part and the imaginary part of that complex number so you don’t need to worry too much about dealing with complex numbers.
Become a Member to join the conversation.