Python 3, an illustrated tour Transcripts
Chapter: Numbers
Lecture: Underscores in Numerics

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One talk about another feature in Python 3.
0:03 This came out pep 515 in Python 3.6
0:06 this is the ability to use underscores in numeric literals.
0:09 When we say literals, sometimes people are confused
0:14 or don't understand what that means.
0:16 A literal is something that is built in into the language
0:19 and that the computer understands.
0:21 So the fact that there are integer literals
0:24 you can type an integer number and Python will know under the covers
0:27 to make an integer for you.
0:29 There are also string literals, simply put a quote around characters
0:33 and Python will create a stream for your under the covers.
0:36 This is in contrast to a class where you might define your own class,
0:40 and in order to create your class, you'll have to call the constructor.
0:43 There won't be a literal way that's built in into the language
0:47 to automatically create one of your classes by using some special syntax.
0:51 The intent of underscores in numbers is
0:55 to be able to group decimals by thousands or hex by words
0:59 just to make them more readable.
1:01 Here's some examples,
1:03 in this case, I have the number 120 million minus 3 million
1:07 and because I've put an underscores where typically
1:10 in English you would see a comma, it's very easy for me to say that
1:15 that's 120 million and 3 million.
1:17 If I had left those underscores out of there,
1:19 it would be a little bit more hard to tell that.
1:22 And in the second example, we have a hex number dead_beef
1:26 and we put in an underscore there to make it slightly more legible.
1:30 One thing to be careful of is that Python doesn't enforce where you put these
1:35 you can put them wherever you want in numbers
1:37 as long as you don't have two underscores together.
1:41 So in this case one, two, three, four, five, six, that's not helping legibility
1:47 by the intent here is to be able to improve legibility.
1:50 So if you're dealing with large numbers or whatnot
1:52 consider using underscores,
1:54 and that will make your numbers a little bit easier to read.