Concepts / Control Flow: if, elif, else Statements

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.

  • Programming
Interactive lab

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

  1. Python evaluates the if condition first.
  2. If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
  3. If none is True, the else branch runs.
  4. 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.

Educational simulation

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 wordsPython’s fixed vocabularyifcontrol structurescoreprogrammer-created nameforcontrol structureVariablesnames created by theprogrammertotalprogrammer-created nameTruespecial valueincludesincludesincludescan includecan include
Which names belong to Python, and which names can the programmer create?

Reserved Words in Control Flow

Definition

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.

CategorySource examplesRole
Control structuresif, for, whileWords used by Python for common control structures
Definitions and typesdef, classReserved language vocabulary identified by Python
Special valuesTrue, False, NoneSpecial 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.

Program sourceReserved wordused as a variable nameErrorprogram rejectedDifferent namechoose a non-reserved namePython checks the namereservedcorrect the name
What happens when a reserved word is used where Python expects a variable name?
Key point

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

Worked example

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.

  1. 1
    Identify ownership

    Ask whether the proposed name belongs to Python’s fixed vocabulary or whether it is a programmer-created word.

  2. 2
    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.

  3. 3
    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.

Result

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.

ifreserved wordVariable nameprogrammer-created nameexpectedErrorprogram rejectedplaced inname conflict
What changes when a Python-reserved word is placed in the variable-name position?

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 like

    A programmer chooses a word without checking whether Python has already reserved it.

    Why it fails

    Python’s reserved words are a fixed part of the language vocabulary and cannot be used as variable names.

    Do this instead

    Choose 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 like

    A reserved word is used as a variable name, with the expectation that Python will decide which meaning to use.

    Why it fails

    Python rejects the program before execution begins.

    Do this instead

    Treat the error as a naming conflict and replace the variable name.

  • Common mistake

    Confusing a variable with a reserved word

    What it looks like

    A programmer assumes that both kinds of words are simply labels chosen by the programmer.

    Why it fails

    Variables are created and named by the programmer, while reserved words have fixed meanings assigned by Python.

    Do this instead

    Separate Python’s vocabulary from your own vocabulary when reading and writing programs.

Best practice

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

EASY

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.

  • Python accepts the name as an ordinary variable

  • Python changes the reserved word into a new variable name

  • Python rejects the program with an error before execution

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

  1. 1
    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.

Related concepts

More concepts starting with C