Control Flow: if, elif, else Statements
This rejection occurs before execution begins. The program does not run with an unpredictable meaning; Python stops and displays an error instead.
Try it: if / elif / else
How Python tests if / elif conditions from the top, runs only the first true branch, and how and / or stop evaluating as soon as the answer is known (short-circuiting).
How it works
- Python evaluates the if condition first.
- If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
- If none is True, the else branch runs.
- and stops at the first False, or stops at the first True — the right side may never be evaluated.
Default run (5 steps): score = 84; A = 90, B = 80, C = 70. Python tests the conditions from the top. … print(grade) shows B.
Loading the simulation…
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.
- 1Identify ownership
Ask whether the proposed name belongs to Python’s fixed vocabulary or whether it is a programmer-created word.
- 2Reject 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.
- 3Choose 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
Common mistake Treating every readable word as available for a variable name
What it looks likeA programmer chooses a word without checking whether Python has already reserved it.
Why it failsPython’s reserved words are a fixed part of the language vocabulary and cannot be used as variable names.
Do this insteadChoose a different programmer-created name when Python rejects the proposed name.
Common mistake Expecting the program to run and reveal what the reserved word means in that context
What it looks likeA reserved word is used as a variable name, with the expectation that Python will decide which meaning to use.
Why it failsPython rejects the program before execution begins.
Do this insteadTreat the error as a naming conflict and replace the variable name.
Common mistake Confusing a variable with a reserved word
What it looks likeA programmer assumes that both kinds of words are simply labels chosen by the programmer.
Why it failsVariables are created and named by the programmer, while reserved words have fixed meanings assigned by Python.
Do this insteadSeparate 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
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
- 1Python 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.