Python Keywords
Python's reserved keywords. None of them can be used as variable, function, or class names.
The full list
| Keyword | What it does |
|---|---|
False True None | The three constants. |
and or not | Logical operators. |
is | Identity comparison. |
in | Membership test / loop iteration. |
if elif else | Conditionals. |
match case | Structural pattern matching (3.10+). |
for while | Loops. |
break continue pass | Loop control / no-op. |
def return yield | Functions and generators. |
lambda | Anonymous function. |
class | Define a class. |
import from as | Module imports + aliases. |
global nonlocal | Write to outer scope. |
try except finally raise | Exceptions. |
with | Context managers (auto-cleanup). |
assert | Internal check; raises AssertionError if false. |
async await | Coroutines for asyncio. |
del | Delete a binding. |
Look it up at runtime
PYTHON
import keyword
print(len(keyword.kwlist), 'keywords:')
print(' '.join(keyword.kwlist))
print(keyword.iskeyword('class')) # True
print(keyword.iskeyword('user')) # False
Soft keywords
Some words (match, case, type) are only reserved in context — you can still name a variable match, but you probably shouldn't.
Tip: If you really need a variable name that collides with a built-in or near-keyword, append an underscore:
class_, type_, list_. The double-underscore version (__class) has a different meaning — don't go there.Example
Example
import keyword
print(len(keyword.kwlist), 'keywords:')
print(' '.join(keyword.kwlist))
Try it Yourself »
Exercise
Inspect the keyword list at runtime via this module.
import
print(
.kwlist)
Same word twice; seven letters.
Discussion
Loading…