Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

PEP 8

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:14 When we go over to python.org—so this is the official page— there’s a lot of PEPs. Those are all the Python Enhancement Proposals. You can read up on the developments in the language.

00:25 Those are proposals before they get accepted, and then they also stay around if they get accepted. This one is number 8, which is about—let’s see what it’s called.

00:36 The Style Guide for Python. Okay.

00:40 So, how do you write good Python code, essentially. We’re going to look at the Naming Conventions section. Let me see.

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:07 And I want to show you that it’s not that scary. You know, all these documents, they seem very long. Look at this. I can just keep scrolling and it keeps going.

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 caseand then this uppercase with underscores, et cetera.

01:58 It just talks descriptively about what are different ways of writing your code.

02:04 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:05 You can always search for stuff, prescriptive, right? Ha.

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.

04:37 That’s my quick introduction to PEP 8. In the next video, we’re going to do a quick recap and go over what we talked about in this section. See you there.

Become a Member to join the conversation.