In this lesson, you’ll get a quick look at PEP 8. This style guide can help you improve the readability of Python code, help you collaborate well with others, and make it easier for them to understand the code you write.
00:00 In this video, I want to give you a quick look into PEP 8, the Python Enhancement Proposal number 8, which talks about the style guide for naming a couple of things, but also for naming variables.
00:49 Naming convention and specifically the Prescriptive: Naming Conventions. So, what should you do, what should you not do. I mostly want to show you this document just so that you know that there is a resource in the Python community that’s the official resource for the language, where you can read up on a lot of this stuff, like how does Python work?
01:14 But it’s actually quite fun to read some of them. There’s some humor in them often. Let me see, now I get lost. Let’s find it again. And it’s really not that scary once you get along. You can always cherry pick and just read something that you’re interested in at the moment.
01:32 So now we’re interested in the naming conventions, and PEP 8 is the place to go for this. You might also hear this name around, and now you know what it’s about. Okay, so Descriptive: Naming Styles here is just about that there’s the possibility to write your code lowercase, uppercase, lowercase with underscores—also called snake case—and then this uppercase with underscores, et cetera.
And then a bit further down, in the Prescriptive: Naming Conventions section it talks about what the Python community suggests on how to use code in Python so that we have a consistent way and make it easier for everyone who writes Python code to read and understand the code of other people and your own code in the future. For example, one very useful tip right here is avoid the character lowercase
l or big
O or big
I, just because it’s very difficult to distinguish what is what, so you might end up mistyping. It could look like a number, like a 1, or the L like a i, or the other way around.
02:39 It just gets really confusing. So this is just a little tip on the side. And if you scroll a bit more down, we have the things that we talked about. Here, for example, Class Names. We want to use the CapWords convention, so a class name in Python should look like that. Yeah.
02:55 So it starts off capitalized and every new word is capitalized again without a space or an underscore in between. And everything else that’s the 80%—or whatever that I was talking about before—if you want to use function and variable names, so most of the code that you’re going to write in Python should be lowercase, and the words should be separated by underscores. Okay?
03:19 That just makes it easier to read. And we looked at a couple of examples. And this is just a suggestion of the Python community. Stick with that for functions, variable names, method names, and instance variables—so, most of the things that you’re talking about.
03:35 Here’s one other thing that we looked at quickly, Constants. So if you write a constant in your program, you can use uppercase characters. That’s the suggestion by Python. All right, so you see there’s a lot of text here.
03:49 Some of this stuff is actually fun to read, so I just want to give you the idea that you know where to look for more information if you’re interested to learn more, and make sure that you’re not scared of looking at these things. Because you always can just pick out pieces of it, you don’t have to read over this whole document.
04:10 Or just “When do you use lowercase?” There you go. And then just scroll through the document. And you’re going to find some interesting information and maybe have some fun on the way. Okay, that’s PEP 8.
04:22 It’s the standard Python Enhancement Proposal for—what was the name again? The Style Guide for Python Code, also called PEP 8. The next time you hear that around, you’ll know where to look for it and you’ll know what it’s about.
Become a Member to join the conversation.