Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Naming Conventions

This lesson is part of a Real Python video course by Martin Breuss.

00:00 Now that you know what variables are and that they’re pointers to objects, in this section, you’re going to take a look at the different variable names that you can use.

00:08 What are the conventions? And we’re also going to take a quick look at PEP 8. We are at section 4 of our course, talking about variable names. And as I mentioned, we’re going to first look at the naming conventions and then peek into PEP 8.

00:22 I’m going to talk about what that is and why you don’t have to be worried about it in just a second after this first section. Let’s get started. So, variable naming conventions.

00:33 You can name things in many ways, okay? And Python is aware of that. But there are still some suggestions on what you should do in order to keep it as structured and as compatible with other code as possible.

00:47 One thing that’s not really a restriction is that a variable in Python can be of any length. So we looked before at a very short one, n = 300.

00:57 Our variable name here is n. It’s very short, it’s just one letter. And that’s perfectly fine. At the same time, it could also be this_is_a_really_really_really_long_name equals, well, let’s call it 400. So this is fine, right?

01:18 It’s going to be pretty annoying if you have to type this out every time that you want to print it, so make sure that you use tab completion as I did just now.

01:26 But it’s no problem for Python. It’s still the same thing as just assigning it a single letter. The length doesn’t matter.

01:35 Next thing, Python variables can be uppercase and lowercase characters or a mix of those too. There is a caveat to this. There’s certain restrictions on when you’re meant to use what.

01:47 Like, they have a certain meaning, but it’s not going to be an error if you use an uppercase or a lowercase variable. For example, we already did lowercase up here. This is all lowercase as well.

02:00 Now I can do an uppercase one. I can say CONSTANT = 42.

02:13 And with autocompletion, you see Python is not complaining using this as a normal variable name. The convention with uppercase is that it should refer to constants, so things that don’t really change. But that’s just as a side note.

02:29 So, we’re using here lowercase, we’re using uppercase. I could do a mix, I could say MIXmeNOT, ha, and that’s fine for Python. It’s not complaining.

02:44 Ah, so, if can type it at all, ha. Okay, there you go. But you want to avoid that. The general way that a variable should look like in Python is using underscores (_) and lowercase characters for most of the names that you’re going to be using. Let’s take a look.

03:03 Here’s an example already. It’s not a great variable name because it’s not very descriptive at all for what the value it is pointing to. But in terms of the formatting, I’m using lowercase and underscores. This is what you want to do.

03:17 Avoid using stuff like that, it just makes it more difficult to read. While it’s not going to produce an error for you—it’s still works—Python is discouraging you from using something like that.

03:27 So a good variable name would be, for example, num_videos = 10. So that’s good, it’s readable. Let’s clear that up.

03:41 This is a good variable name. It’s easily readable, it’s clear what it’s about, it’s descriptive of the value that it points to. Let’s say at the beginning, when you’re starting your programming journey with Python at the beginning, in 80% of the cases you’re going to be fine if you default to a variable naming scheme like that. You use lowercase letters, you use underscores to separate different words, and you try to be descriptive.

04:08 So, this also shows us underscores are fine in a variable name. What else? We can use digits in our variable names, but not at the beginning. So, while this works…

04:24 Not a problem. It’s all fine. I cannot say… For example, this is going to be an error. You see? We’re getting a SyntaxError, which just tells us in Python that our variable name cannot start with a number.

04:41 Numbers are fine. They can be part of a variable name, they can be at the end or somewhere in the middle.

04:55 Not a problem. But not at the beginning.

05:02 And finally, something that’s new with Python 3, you can use Unicode characters for your variable names as well. So what we can do now, finally, is we can assign variable names like this.

05:14 I can say voilà! Let’s write it correctly, like this.

05:23 "FRANCE"! And Python’s fine with that. Using a Unicode character like this à with the accent on it would not have worked in Python 2.

05:34 That’s a new thing that works with Python 3, but now you can use it as parts of variable names and that’s perfectly fine. It opens it up to a couple more languages that might not have been able to be programmed in using the characters that they use normally in their language.

05:48 So, that’s a great improvement in Python 3, and just another reason for using Python 3.

05:56 All right, so these are the variable name conventions. We just went over them quickly now. And we’re going to do a recap in just a second. But first, in the next video, I want to show you PEP 8, which is a document where all of these conventions are written down and where you can always go back to check on them. See you there in the next video.

You must own this product to join the conversation.