iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Python Keywords

Python's reserved keywords. None of them can be used as variable, function, or class names.

The full list

KeywordWhat it does
False True NoneThe three constants.
and or notLogical operators.
isIdentity comparison.
inMembership test / loop iteration.
if elif elseConditionals.
match caseStructural pattern matching (3.10+).
for whileLoops.
break continue passLoop control / no-op.
def return yieldFunctions and generators.
lambdaAnonymous function.
classDefine a class.
import from asModule imports + aliases.
global nonlocalWrite to outer scope.
try except finally raiseExceptions.
withContext managers (auto-cleanup).
assertInternal check; raises AssertionError if false.
async awaitCoroutines for asyncio.
delDelete 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)

Test yourself

Q1. Inspect at runtime via…
Q2. Which is a Python keyword?
Q3. Avoid using these names as variables…

Discussion

Loading…