In this lesson you will
- Write if, elif and else branches with comparisons and and/or
- Repeat work with for, range and while
- Control loops with break and continue and read indentation errors
if, elif and else
An if line ends with a colon, and the block under it is indented. Python checks the conditions from top to bottom and runs only the first block whose condition is true; elif means "else if", and else catches everything left. In the example, an order of 180 AED is under the 200 AED free-delivery limit, Sharjah matches the second condition, so the fee is 15 and the else never runs. Use == to compare, because a single = assigns, and Python rejects if fee = 0: as a syntax error.
Conditions combine with the words and, or and not, not with symbols such as &&. Any value can act as a condition: 0, 0.0, an empty string "", an empty list [] and None count as false, and almost everything else counts as true. That is why bool("0") printed True: it is a string with one character, not the number zero. Code such as if name: relies on this to check that a text field is not empty.
Console
- Console messages, errors, form data and clicked links appear here.
This printed Delivery to Sharjah: 15 AED, then True True True and False False True False. Set order_total = 250 and the fee becomes 0; set emirate = "Fujairah" with 180 and it becomes 25.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 2 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Does Python have a switch statement?
Since Python 3.10 there is match with case blocks, which can do more than a classic switch. For a few simple choices, an if, elif chain is still the most common and readable option.
Why does Python use indentation instead of braces?
It was a design choice for readability: the layout you see is the structure the computer follows, so code cannot look nested while it is not. Editors indent with four spaces automatically when you press Enter after a colon.
Is there a do-while loop in Python?
No. Write while True: with the work first and an if ...: break at the end of the block. That runs the body at least once, like do-while in other languages.
How do I loop with an index and the item together?
Use enumerate(): for i, city in enumerate(cities, start=1): gives 1 Dubai, 2 Doha and so on. It is clearer than range(len(cities)) with cities[i].
What happens if my loop never ends in the browser?
The example frame stops responding because Python is busy. Reload the page, fix the line that should change the condition, and run again. On your own computer, press Ctrl+C in the terminal to stop a running script.
Sources
Facts in this lesson were checked against these official pages on 2 December 2026.
- Python docs: More Control Flow Tools
- Python docs: Truth Value Testing
- Python docs: range
- PEP 8: Indentation
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.