Skip to content

Computer Science · Ch 5 — Getting Started with Python

Python Keywords

5.2

Python Keywords

Some words in Python are not available for us to use as names — the language has already claimed them. These are Python's keywords: reserved words, each carrying a specific meaning for the Python interpreter. A keyword may be used in a program only for the purpose for which it has been defined; it cannot be repurposed as, say, a variable name.

Because Python is case-sensitive, every keyword must be written exactly as defined — True is a keyword, but true or TRUE is not.

The Python keywords (Table 5.1)

Falseclassfinallyisreturn
Nonecontinueforlambdatry
Truedeffromnonlocalwhile
anddelglobalnotwith
aselififoryield
assertelseimportpass
breakexceptinraise

A few of these are already familiar from this chapter's examples — True and False (the boolean constants), None (the "no value" object), and in/not/and/or/is (operators met later in the chapter). Others, such as if, for, while, def and import, drive the control structures and functions taught in later chapters.

What "reserved" means in practice

Attempting to use a keyword as an identifier produces a syntax error:

>>> class = 11
SyntaxError: invalid syntax
``` …
Table 5.1Python keywords
Falseclassfinallyisreturn
Nonecontinueforlambdatry
Truedeffromnonlocalwhile
anddelglobalnotwith
aselififoryield