Python Keywords Explained

Learn what Python keywords are, which words are reserved, and why you cannot use them as variable or function names.

5 min read

Every programming language has a small set of words that belong to the language itself. These words are called keywords, and they are the building blocks that Python's parser uses to understand the structure of your code. When you write if, for, def, or return, you are using keywords that Python recognizes and treats in a special way. You have already seen several of them in earlier articles without necessarily realizing they belong to a separate category from the names you invent for your own variables and functions.

Keywords are reserved, which means you cannot use them as names for your own variables, functions, classes, or any other identifier. If you try to write class = "Biology 101", Python will refuse with a SyntaxError because class is a keyword that starts a class definition. Understanding which words are off-limits helps you avoid frustrating errors and also teaches you what capabilities the language offers natively.

The 35 hard keywords

Python 3.x has a fixed set of 35 keywords that are always reserved, regardless of context. They are the words that define Python's grammar: control flow, logical operations, function and class definitions, exception handling, imports, and variable scoping. Here they are, grouped by the role they play in your programs:

CategoryKeywords
Control flowif, elif, else, for, while, break, continue, pass
Logical operatorsand, or, not, in, is
ValuesTrue, False, None
Functions and classesdef, return, lambda, yield, class
Exception handlingtry, except, finally, raise, assert
Imports and modulesimport, from, as
Variable scopeglobal, nonlocal
Asyncasync, await
Context and otherwith, del

You do not need to memorize this list. You will learn each keyword naturally as you encounter the feature it belongs to. The important rule for now is simple: if Python gives you a SyntaxError on a line that looks correct, check whether you accidentally used a keyword as a variable name. This is especially common with words like list, type, from, and import, which feel like normal English words that would make reasonable variable names but are in fact reserved.

Soft keywords

Starting with Python 3.10, the language introduced the concept of soft keywords. Unlike hard keywords, which are always reserved, soft keywords are only treated as keywords in specific grammatical positions. Outside those positions, they can be used as regular identifiers. As of Python 3.14, the soft keywords are match, case, and underscore (when used inside a match statement), plus type (when used in a type statement).

This means you can write match = 3 as a normal variable assignment and Python will not complain, even though match acts as a keyword inside a match statement block. Soft keywords were introduced so that new language features do not break existing code that used those words as variable names. It is still best practice to avoid using soft keywords as identifiers, because it makes your code harder to read and can cause confusion when the same word means different things in different lines.

Checking keywords programmatically

Python's standard library includes a module called keyword that lets you work with the keyword list in your own code. This is useful when you are building a tool that validates user input, or when you just want to double-check whether a name you have in mind is safe to use:

pythonpython
import keyword
 
print(keyword.iskeyword("class"))   # True
print(keyword.iskeyword("student")) # False

The kwlist attribute returns the full list of hard keywords as strings. The iskeyword function checks a single name and returns True if it is reserved. This is especially helpful when you are generating Python code dynamically or building a learning tool that needs to flag invalid identifier names.

Keywords versus built-in functions

A common point of confusion for beginners is the difference between keywords and built-in functions. Keywords are part of the language grammar itself. Built-in functions like print, input, len, and int are regular names that Python pre-defines for you, but they are not reserved. You can technically reassign them, though doing so is almost always a bad idea. After writing print = "overwritten", the name print no longer refers to the built-in function, and calling print("Hello") would raise a TypeError.

Python lets you overwrite built-in names, but it is considered a mistake. As you read more Python code and learn about Python identifiers and naming rules, you will develop an instinct for which names are safe to use and which ones belong to the language.

Why keywords matter for beginners

Knowing the keyword list saves you debugging time. When you see a SyntaxError pointing at a line that seems perfectly reasonable, one of the first things to check is whether you unknowingly used a reserved word. Errors on lines like from = "London" are confusing until you realize that from is a keyword used in import statements.

Keywords also serve as a map of what Python can do. Scanning the list, you can see the language's core capabilities: it has conditionals, loops, functions, classes, exceptions, imports, and async support. Each keyword you learn unlocks a new category of programs you can write, and the progression through them mirrors the structure of this tutorial series, starting with Python syntax and indentation and building toward more advanced concepts.

Rune AI

Rune AI

Key Insights

  • Python has 35 hard keywords that are permanently reserved and cannot be used as identifiers.
  • Attempting to assign to a keyword like class or for produces a SyntaxError.
  • Soft keywords like match and case are only reserved in specific grammatical contexts.
  • The keyword module lets you programmatically check whether a word is reserved.
RunePowered by Rune AI

Frequently Asked Questions

How many keywords does Python have?

Python 3.14 has 35 hard keywords: False, await, else, import, pass, None, break, except, in, raise, True, class, finally, is, return, and, continue, for, lambda, try, as, def, from, nonlocal, while, assert, del, global, not, with, async, elif, if, or, yield. There are also soft keywords like match, case, and type that are only reserved in specific contexts.

How can I see all Python keywords programmatically?

Import the keyword module and print keyword.kwlist. You can also call keyword.iskeyword('name') to check if a specific name is reserved.

Can I use a keyword as a variable name if I change the capitalization?

Yes. Python keywords are case-sensitive. If, IF, and Class are not keywords and can be used as identifiers, though doing so is confusing and not recommended.

Conclusion

Keywords are the fixed vocabulary of the Python language. You cannot repurpose them as variable or function names, and trying to do so produces an immediate syntax error. Knowing the list of reserved words helps you choose valid names from the start and makes reading other people's code easier because you recognize which words have special meaning.