Skip to content

Python Conditions and Loops: if, elif, else, for, range, while, break and continue

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

A program becomes useful when it can choose between paths and repeat work. In Python both rely on one rule that surprises people coming from other languages: the lines that belong to an if or a loop are the ones indented under it, usually by four spaces. There are no curly braces. This lesson builds a delivery fee calculator with if, elif and else, prints a times table with for and range(), counts the months to a savings goal with while, and skips or stops loops with continue and break. Every example runs in your browser, so change the numbers and watch the result change.

Quick answer

Python chooses with if condition:, optional elif other: branches and an optional else:; the indented lines under each run only when it is chosen. Comparisons are == != < > <= >=, combined with and, or and not. for item in sequence: repeats once per item, and range(start, stop, step) gives numbers up to but not including stop. while condition: repeats until the condition is false. break leaves a loop and continue skips to the next round.

35 min read and practice · Content checked on 2 December 2026

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.

Try it: A delivery fee with if, elif and else
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.