Tutorials Logic, IN info@tutorialslogic.com

Python Keyword Reference: Reserved Words and Their Jobs

Python Keywords

Keywords are reserved words that Python uses for syntax.

You cannot use a keyword as a variable, function, class, or module name.

The easiest way to learn keywords is by job: control flow, functions, classes, imports, exceptions, async code, and boolean/null values.

Control Keywords

Control keywords decide which block runs, how loops continue, and when a function returns.

Keyword Job
if / elif / else Choose between branches
for / while Repeat work
break / continue Control loop flow
return Send a value back from a function

Keyword in Flow

Keyword in Flow
score = 82

if score >= 50:
    print("pass")
else:
    print("try again")
Output
pass

if and else are keywords because they define Python syntax.

Definition Keywords

Definition keywords create reusable names for functions, classes, imports, and exception handling.

Keyword Job
def Create a function
class Create a class
import / from / as Bring code into the current file
try / except / finally / raise Handle or signal errors

Avoid Keyword Names

Avoid Keyword Names
import keyword

print(keyword.iskeyword("class"))
print(keyword.iskeyword("course"))
Output
True
False

The keyword module can check whether a word is reserved.

Complete Reserved Keyword Map

Reserved keywords are part of Python grammar and cannot be used as ordinary variable, function, class, or attribute names. Learn them by job instead of as an alphabetical wall; the grouping makes syntax easier to recall and reveals which words belong together.

  • Constants: False, None, True.
  • Boolean and membership operations: and, in, is, not, or.
  • Branches and pattern flow: if, elif, else, match, case.
  • Loops and loop control: for, while, break, continue.
  • Functions and generators: def, lambda, return, yield.
  • Classes and scope: class, global, nonlocal.
  • Imports and aliases: import, from, as.
  • Exceptions and cleanup: assert, raise, try, except, finally, with.
  • Asynchronous code: async, await.
  • Other statements: del, pass.

Soft Keywords and the keyword Module

Soft keywords act as grammar words only in specific contexts, so they may still be valid identifiers elsewhere. Current Python versions use match, case, and _ in structural pattern matching, and type in a type-alias statement. Check the documentation for the Python version your project supports because this list can evolve.

The standard keyword module reports the interpreter's actual reserved and soft-keyword sets. Use it in code generators, schema-to-Python tools, and linters before creating identifiers. Appending an underscore, such as class_, is the conventional repair for a generated name that collides with a keyword.

Ask the running interpreter

Ask the running interpreter
import keyword

print(keyword.iskeyword("class"))       # True
print(keyword.issoftkeyword("match"))  # True
print(keyword.kwlist)
print(keyword.softkwlist)

The module reflects the exact interpreter running the program, which is safer than copying a list from another Python release.

Before you move on

Can You Use Python Keywords?

5 checks
  • You can explain why keywords cannot be variable names.
  • You can group keywords by their purpose.
  • You can recognize control-flow, definition, import, and exception keywords.
  • You can use keyword.iskeyword to check suspicious names.
  • You can fix SyntaxError caused by reserved names.

Keyword Role Confusion

  • Using a keyword as a variable

    Rename class, for, from, or try to a meaningful non-keyword name such as class_name or source.
  • Memorizing without context

    Learn keywords inside examples: if in decisions, def in functions, class in OOP, try in errors.
  • Confusing keywords and built-ins

    Keywords are syntax. Built-ins such as len and print are callable names.

Try this next

Identify Keyword Roles

0 of 3 completed

  1. Make four groups: flow, functions/classes, imports, and errors. Place five keywords into the groups.
  2. Use keyword.iskeyword on five names you might use in a program.
  3. Write three invalid variable names that are keywords, then rename them clearly.

Questions About Python Keywords

No. They are built-in functions. Keywords are reserved syntax words such as if, for, def, class, and return.

Yes, language versions can add syntax over time. Use the keyword module to check the interpreter you are using.

Python tries to read it as syntax, not as your custom variable name.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.