Branches (if/elif/else)
Module 1 — What is a branch?
During execution, decide which block of code should run.
Definition
One Branches (English conditional statement) makes your program react differently depending on whether a condition is true or false. The simplest form is this if-Statement: The indented block is running only then if the condition True :.
Syntax:
if <Bedingung>:
<Anweisungen, falls Bedingung wahr>
You create a condition Comparative operators — they always deliver True or False:
== (same — double equal sign!), != (unequal),
<, <=, >, >=. For “even/odd” you also need the remainder operator
% (e.g. B. zahl % 2 == 0 = remainder 0 = even).
Example 1.1 — a single if query
Loading Python…Tests
Module 2 — if and else
Exactly one block is running - either that if-branch or the else- Two.
Structure
if <Bedingung>:
<Anweisungen, falls wahr>
else:
<Anweisungen, falls falsch>
The else-branch has no own condition — it catches everything that the if- condition not fulfilled. So exactly one of the two blocks always runs, never both and never neither.
Example 2.1 — positive or not positive
Loading Python…Tests
Example with modulo
A very common branch: Is the number even or odd? The modulo operator
% returns the remainder of a division — zahl % 2 is 0 for even numbers, 1 for odd.
Example 2.2 — even or odd?
Loading Python…Tests
Module 3 — elif and the order of the branches
More than two options — and why the order matters.
Structure
if <Bed-1>:
<Block A>
elif <Bed-2>:
<Block B>
elif <Bed-3>:
<Block C>
else:
<Block D>
Python checks the conditions of top to bottom. As soon as one of these
True results, the associated block is executed - all others elif
and that else they are skipped. So it's working always exactly one branch.
Example 3.1 — Translate grade into evaluation
Loading Python…Tests
Order is important
If your elif-conditions overlapping, the first matching condition takes precedence. A classic example is dividing the day into periods using “less than”.
if uhrzeit < 12:
print("Morgen")
elif uhrzeit < 18:
print("Mittag")
else:
print("Abend")
For uhrzeit = 14 is too uhrzeit < 18 true — but the first condition (< 12) was wrong, so the elif-Twig taken. If you put the branches in the wrong order (< 18 above), you incorrectly get “lunch” for 10 a.m.
Example 3.2 — Morning, Noon, Evening
Loading Python…Tests
Module 4 — Common pitfalls
= vs. ==, indentation, forgotten else.
Trap 1: = the ==
A simple equality check is == (double equal sign).
= is that Allocation. In Python it is if zahl = 5: a syntax error — in other languages it would be a silent Error. Still a typical typo.
Trap 2: Incorrect indentation
Python recognizes blocks only at the indentation. What the if heard, must be indented; What is normal again in the left column is running always. A common beginner error is placing output in the wrong if-Block, but is accidentally on the edge and then appears even in the negative case.
Trap 3: else forgotten
A single one if without else is allowed - but then happens in the negative case nothing. If you want to make sure that always
a message comes up, you need it else- Two.
Example 4.1 — what happens without else?
Loading Python…Tests
TipIn the exercises
Most programming tasks read their value input() (see “Variables” module) and classify it — the check automatically feeds in the input and compares your output character by character. Therefore, output exactly the word required, without any additional text. Part 3 is deliberately different: there you define the value yourself (none input()) and the check checks that your output matches the selected value - so you can see that not every exercise needs an input.
Sources & further literature
Where the knowledge imparted here is officially documented.
Official Python documentation
- Python Language Reference — The
ifstatement: docs.python.org/3/reference/compound_stmts.html#the-if-statement - Python tutorial — 4.1.
ifStatements: docs.python.org/3/tutorial/controlflow.html#if-statements
Tutorials & Manuals
- Real Python — Conditional Statements in Python: realpython.com/python-conditional-statements
- Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023 — Chapter on branches.
- Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021 — “Conditional Instructions” section.