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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Reserved Keywords

There is one more restriction on identifier names. The Python language reserves a small set of keywords that designate special language functionality. No object can have the same name as a reserved word.

In Python 3.6, there are 33 reserved keywords:

Python
Keywords
     
False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not
class from or
continue global pass

You can see this list any time by typing help("keywords") to the Python interpreter. Reserved words are case-sensitive and must be used exactly as shown. They are all entirely lowercase, except for False, None, and True.

00:00 Welcome to this video about reserved keywords. Those are a couple of words that you can’t use as variable names in Python, and the reason is because they have a certain meaning in the language.

00:10 They just instruct Python to do certain things, so we can’t use them as variable names because we’d just make it really confusing for Python and that’s not how a programming language works.

00:19 So, they’re special words, as I just mentioned. They’re going to produce a SyntaxError when you try to assign a value to them as a variable name, and I’ll show you how to get help about what are the different keywords that exist, and I’ll show you two different ways of doing that.

00:35 So without further talk, let’s get started and head over to the Python console. Let’s do a quick recap. So if you try to assign, for example, 42 as a variable name over here, this is not possible because it’s just a number. And a SyntaxError pops up, Python tells us this is not allowed in the language.

00:56 And we’ll get the same thing when using a keyword as a variable name. This is going to give us a SyntaxError. Just in the same way, Python is telling us this is not allowed, that’s just not how variable names work. Okay, so, how do I know that for is a keyword and that I can’t use it?

01:12 That might be surprising if you don’t know that these keywords exist and what they are. Let’s get some help. You can simply type help(), and then as a string, you say "keywords".

01:23 You want to get help on the Python keywords, and Python nicely prints out a list of all the Python keywords that exist. It also tells us Enter any keyword to get more help,

01:35 which just means we can use the same syntax up here to get help on a specific one. So if we want to know more about False, I can type help("False").

01:46 And then there’s a nice description within Python that explains what is it, and how is it used. If you want to get back out of this, you just have to press the Q character. Okay, so here’s the list of keywords. It’s quite a lot of them, right?

02:01 I think at the current 3.7, it’s 35 keywords if I’m not mistaken. And all of them—you might get very familiar with them if you work with Python because a lot of them you just keep using over and over again.

02:16 But for the beginning it might be confusing. So, what’s a keyword? If you don’t always want to type help("False"), there’s a different way of getting this list as well.

02:25 You can import keyword,

02:29 that’s a module in Python that’s part of the standard library. And then you can get the list by saying keyword.kwlist() (keyword list). And now we get it as a list.

02:39 Now we can actually quickly find out how many there are.

02:47 All right. So 35, as I thought. There are currently 35 keywords. Another thing that you can do here, which is additional to just getting the list that we also got before—you can ask whether something is a keyword. So I can say keyword.iskeyword() and then pass in something. I can say “Is False a keyword?” And I get as a response True, yeah. Okay, so that’s a keyword. That’s something I can’t use. What about n, that we’ve been using all the time before? False, so this is fine. This is a fair variable name to use, whatever. Can I use hello?

03:29 I can. That’s great. Okay, so if you want to check up on something, you can use it with this keyword module, import keyword, and then say keyword.iskeyword(), pass in what you want to know about, and it’s going to tell you whether it’s a keyword or not.

03:43 But probably, it’s going to be fine to just type help("keywords"), see the list, and you’ll get familiar with most of them pretty quickly because it’s just stuff that you keep using over and over again when you work a lot with Python.

03:59 So, the easiest way to remember how to get help on keywords is to type help("keywords") into your console. It’s going to tell you which keywords exist.

04:08 So, in the section Reserved Keywords, we talked about that keywords are special words that have a meaning in Python, so we cannot use them as variable names.

04:15 We’ll get a SyntaxError if we try to do that. And I also showed you two ways of getting help related to keywords. That’s all that I want to say about in here.

04:24 And that means we’ve gotten to the end of our table of contents. We talked about variable assignment, object references, object identity, variable names, and now also reserved keywords. So in the final section, in the upcoming video, we’re going to go over all of those quickly again, just to wrap it up and make sure that the most important takeaways, that we revisit them quickly. And yeah, I hope you had fun so far.

04:47 Let’s do the conclusion. Thanks for joining this course, and see you in this final conclusion video.

John DB on Dec. 11, 2019

Maybe some commentary about this?

import keyword
print(keyword.iskeyword('keyword'))
...
-> False

It seems to me, a bigger problem with keyword clashes is not the small fixed list of 36 items above, but scenarios like this:

Suppose I have a bunch of working code, including snippets copied from StackOverflow:

x = 5
print("-> x:", str(x))

str = "/usr/bin/ls"
print ("-> file:", str)
...
-> x: 5
-> file: /usr/bin/ls

In the next dev cycle, I add more code almost identical to that which works above, eg:

x = 5
print("-> x:", str(x))

str = "/usr/bin/ls"
print ("-> file:", str)

y = 5
print("-> y:", str(y))

But… BOOM!

Traceback (most recent call last):
  File ".../scratch.py", line 9, in <module>
    print("-> y:", str(y))
TypeError: 'str' object is not callable

-> x: 5
-> file: /usr/bin/ls

So… how can I find out about “almost keywords” like str which can suddenly become keywords (or keyword-like) with inadvertent use – thus turning my erstwhile “stable code” into a walking time-bomb?

John DB on Dec. 11, 2019

Ah, some investigation reveals more insight. I suggest you add a note about the difference between keywords, built-ins, and reserved words.

Here’s some StackOverflow wisdom about the topic: stackoverflow.com/questions/22864221/is-the-list-of-python-reserved-words-and-builtins-available-in-a-library

Now I realize that pylint is helpful here:

""" test """
str = "/usr/bin/ls"
print ("-> file:", str)
...
-> file: /usr/bin/ls

And here’s the desired warning:

$ pylint  .../scratch.py
No config file found, using default configuration
************* Module scratch
W:  2, 0: Redefining built-in 'str' (redefined-builtin)
C:  2, 0: Constant name "str" doesn't conform to UPPER_CASE naming style (invalid-name)
-------------------------------------------------------------------
Your code has been rated at 0.00/10

Become a Member to join the conversation.