Python 3, an Illustrated Tour Transcripts
Lecture: Underscores in Numerics
0:00 One talk about another feature in Python 3. This came out pep 515 in Python 3.6 this is the ability to use underscores in numeric literals.
0:10 When we say literals, sometimes people are confused or don't understand what that means. A literal is something that is built in into the language
0:20 and that the computer understands. So the fact that there are integer literals you can type an integer number and Python will know under the covers
0:28 to make an integer for you. There are also string literals, simply put a quote around characters
0:34 and Python will create a stream for your under the covers. This is in contrast to a class where you might define your own class,
0:41 and in order to create your class, you'll have to call the constructor. There won't be a literal way that's built in into the language
0:48 to automatically create one of your classes by using some special syntax. The intent of underscores in numbers is
0:56 to be able to group decimals by thousands or hex by words just to make them more readable. Here's some examples,
1:04 in this case, I have the number 120 million minus 3 million and because I've put an underscores where typically
1:11 in English you would see a comma, it's very easy for me to say that that's 120 million and 3 million. If I had left those underscores out of there,
1:20 it would be a little bit more hard to tell that. And in the second example, we have a hex number dead_beef
1:27 and we put in an underscore there to make it slightly more legible. One thing to be careful of is that Python doesn't enforce where you put these
1:36 you can put them wherever you want in numbers as long as you don't have two underscores together.
1:42 So in this case one, two, three, four, five, six, that's not helping legibility by the intent here is to be able to improve legibility.
1:51 So if you're dealing with large numbers or whatnot consider using underscores, and that will make your numbers a little bit easier to read.