Computer Science · Ch 5 — Getting Started with Python
Python Keywords
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)
False | class | finally | is | return |
None | continue | for | lambda | try |
True | def | from | nonlocal | while |
and | del | global | not | with |
as | elif | if | or | yield |
assert | else | import | pass | |
break | except | in | raise |
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
``` …
| False | class | finally | is | return |
|---|---|---|---|---|
| None | continue | for | lambda | try |
| True | def | from | nonlocal | while |
| and | del | global | not | with |
| as | elif | if | or | yield |