Python Reference ยท Statements and Keywords
The list of Python keywords
Complete list of Python keywords and soft keywords, what each one does, how to list them with the keyword module, and why you cannot use them as names.
Keywords are words with a fixed meaning in Python's grammar. You cannot use them as variable, function or class names, because the parser reserves them for statements, operators and literals. Python 3.14 has 35 reserved keywords; the standard keyword module reports the list for your running version.
A few more words, the soft keywords, are special only in certain positions and remain valid names elsewhere.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Reserved keywords by purpose
| Group | Keywords |
|---|---|
| Constants | True, False, None |
| Logic and tests | and, or, not, in, is |
| Conditions | if, elif, else |
| Loops | for, while, break, continue |
| Functions | def, return, lambda, yield |
| Classes | class |
| Exceptions | try, except, finally, raise, assert |
| Context | with, as |
| Imports | import, from |
| Scope | global, nonlocal, del |
| Async | async, await |
| Placeholder | pass |
That is 35 words in total. keyword.kwlist prints them sorted, with False, None and True first.
Soft keywords
| Word | Where it is special |
|---|---|
match, case |
In match statements (3.10+). |
_ |
As the wildcard pattern inside case. |
type |
At the start of a type X = ... statement (3.12+). |
Soft keywords are fine as variable names in ordinary code, though reusing type shadows the built-in function.
Usage notes
- Built-ins are not keywords.
print,len,list,str,idandinputare ordinary names that you can rebind, usually by mistake. Avoid it. - Case matters.
True,FalseandNoneare capitalized;trueandnoneare plain identifiers. - Attribute access. A keyword cannot appear as an attribute name in source (
obj.classis a syntax error); usegetattr(obj, "class")or rename. - Keyword arguments can not be keywords either: write
from_=orclass_=and use a trailing underscore, as PEP 8 recommends. - The list grows.
asyncandawaitbecame real keywords in 3.7. Old code using them as names breaks on new versions. - Removed:
printandexecwere statements in Python 2 and are normal functions now. - Check programmatically with
keyword.iskeyword(name)when generating code or accepting identifiers from users.