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

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds 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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Misspelling, Missing, or Misusing Python Keywords

Here are resources for more information about Python keywords:

00:00 Misspelling, Missing, or Misusing Python Keywords. Python keywords are a set of protected words that have special meaning in Python. These are words that you can’t use as identifiers, variables, or function names in your code.

00:15 They’re a part of the language and can only be used in the context that Python allows. There are three common ways that you can mistakenly use keywords: misspelling a keyword, missing a keyword, or misusing a keyword.

00:32 If you misspell a keyword in your Python code, then you’ll get a SyntaxError. For example, here’s what happens if you spell the keyword for incorrectly.

00:46 The message reads SyntaxError: invalid syntax, but that’s not very helpful. The traceback points to the first place where Python could detect that something was wrong.

00:56 To fix this sort of error, make sure that all of your Python keywords are spelled correctly. Another common issue with keywords is when you miss them altogether.

01:09 Once again, the exception message isn’t that helpful, but the traceback does attempt to point you in the right direction. If you move back from the caret, then you can see that the in keyword is missing from the for loop syntax.

01:24 You can also misuse a protected Python keyword. Remember, keywords are only allowed to be used in specific situations. If you use them incorrectly, then you’ll have invalid syntax in your Python code.

01:37 A common example of this is the use of continue or break outside of a loop. This can easily happen during development when you’re implementing things and happen to move logic outside of a loop.

02:01 Here, Python does a good job of telling you exactly what’s wrong with this example. The message 'break' outside of loop helps you figure out exactly what to do.

02:10 If this code were in a file, then Python would also have the caret pointing right to the misused keyword. Here, you can see that the misuse of continue follows a similar pattern of generating a SyntaxError, with a different message.

02:28 Another example is if you attempt to assign a Python keyword to a variable or use a keyword to define a function.

02:39 When you attempt to assign a value to pass, or when you attempt to define a new function called pass, you’ll get a SyntaxError and see the invalid syntax message again.

02:49 It might be a little harder to solve this type of invalid syntax in Python code because the code looks fine from the outside. If your code looks good, but you’re still getting a SyntaxError, then you might consider checking the variable name or function name you want to use against the keyword list for the version of Python that you’re using.

03:07 The list of protected keywords has changed with each new version of Python. For example, in Python 3.6 you could use await as a variable name or function name, but as of Python 3.7, that word has been added to the keyword list.

03:20 Now if you try to use await as a variable or function name, this will cause a SyntaxError if your code is running on Python 3.7 or later.

03:29 Another example of this is print, which differs for Python 2 versus Python 3. print is a keyword in Python 2, so you can’t assign a value to it.

03:40 In Python 3, however, it’s a built-in function that can be assigned values. You can run the following code to see the list of keywords in whatever version of Python you’re running.

03:55 keyword also provides the useful iskeyword() function. If you just need a quick way to check the pass variable, then you can use the following code.

04:07 This code will quickly tell you if the identifier that you’re trying to use is a keyword or not.

Become a Member to join the conversation.