Control Flow: if, elif, else Statements
Reserved words are a fixed set of 35 words that have unchangeable, singular meanings to Python and form the core of the language's vocabulary.
A Name Can Have Two Owners
When you write a Python program, some words already belong to Python. Other words are names that you create for your own data. The important question is not only whether a word looks readable, but also whether Python has already assigned that word a special meaning.
Python’s reserved words form a fixed vocabulary of 35 words. Each has an unchangeable, singular meaning to Python. Variables belong to the programmer’s vocabulary: they are names the programmer creates to store and work with data. Reserved words tell Python what role a part of a program has, while variables provide names for the data the program processes.
Reserved Words in Control Flow
A reserved word is one of Python’s fixed set of 35 words with an unchangeable, singular meaning in the language.
The source identifies common control-structure words such as if, for, and while, along with def and class. It also identifies True, False, and None as special values. These words are not ordinary labels that can be reassigned to your own data. They are part of Python’s language vocabulary.
| Category | Source examples | Role |
|---|---|---|
| Control structures | if, for, while | Words used by Python for common control structures |
| Definitions and types | def, class | Reserved language vocabulary identified by Python |
| Special values | True, False, None | Special values identified as part of Python’s reserved vocabulary |
Examples of reserved-word roles identified in the source material
Why Names Get Rejected
Python must be able to recognize the special role of each reserved word. If a reserved word could also freely become a variable name, the same word could be asking Python to use its language meaning in one place and to treat the word as programmer-created data in another. Python prevents that conflict by rejecting an attempt to use a reserved word as a variable name.
This rejection occurs before execution begins. The program does not run with an unpredictable meaning; Python stops and displays an error instead.
Reading the Naming Boundary
Choosing a name for a stored value
A programmer wants to name a variable for a result. One possible name is already part of Python’s reserved vocabulary, while another name is created by the programmer.
Identify ownership: Ask whether the proposed name belongs to Python’s fixed vocabulary or whether it is a programmer-created word.
Reject the conflict: If the proposed name is a reserved word, it cannot be used as a variable name. Python rejects the program and reports an error.
Choose a variable name: Use a different programmer-created name for the value. Variables provide names for data, but they cannot take the place of reserved words.
The useful distinction is ownership: reserved words belong to Python, while variables belong to the programmer. A variable name must not be one of Python’s reserved words.
The important lesson is not that a particular word looks unusual. The lesson is that a word’s role is determined by Python’s vocabulary. A programmer-created name can describe data, but a reserved word retains its language meaning and cannot be claimed as a variable name.
Mistakes Beginners Make
Treating every readable word as available for a variable name
Python’s reserved words are a fixed part of the language vocabulary and cannot be used as variable names.
Fix:
Choose a different programmer-created name when Python rejects the proposed name.Expecting the program to run and reveal what the reserved word means in that context
Python rejects the program before execution begins.
Fix:
Treat the error as a naming conflict and replace the variable name.Confusing a variable with a reserved word
Variables are created and named by the programmer, while reserved words have fixed meanings assigned by Python.
Fix:
Separate Python’s vocabulary from your own vocabulary when reading and writing programs.
When a naming error appears, first ask whether the proposed variable name is one of Python’s reserved words. Many Python environments highlight reserved words in a special color, often blue or purple, which can help you recognize that the word belongs to Python rather than to your program.
Naming Practice
For each proposed name, decide which vocabulary it belongs to: Python’s reserved vocabulary or the programmer’s variable vocabulary. Then explain what the programmer should do if the name is reserved: if, score, True, total, class.
Hints
- The source explicitly identifies if, True, and class as examples of reserved words.
- Variables are words created and named by the programmer.
- A reserved word cannot be used as a variable name.
What do you think happens?
Predict what Python does when a programmer attempts to use a reserved word as a variable name.
Reveal answer
Answer: Python rejects the program with an error before execution.
Reserved words have fixed meanings in Python. Allowing one to become a variable name would create a naming conflict, so Python stops and reports an error.
The Rule to Carry Forward
- Python has a fixed set of 35 reserved words with unchangeable meanings. Words such as if, for, while, def, class, True, False, and None belong to Python’s vocabulary. Variables belong to the programmer’s vocabulary and are used to store and work with data. A reserved word cannot be used as a variable name; Python catches that conflict before execution and reports an error. When choosing a variable name, always leave Python’s reserved vocabulary untouched.
Key Takeaways
- Reserved words are Python’s fixed vocabulary of 35 words with unchangeable meanings.
- Control-structure words such as if, for, and while are examples of reserved vocabulary identified in the source.
- Variables are programmer-created names used to store and work with data.
- A reserved word cannot be used as a variable name.
- Python rejects this naming conflict before execution begins, so choose a different variable name.