Control Flow
Beginner

Control Flow

Programs become useful when they can decide and repeat. These lessons show each idea as a diagram first, then as code you can run — from simple if statements to match, range(), loop else and nested loops.

  1. 1Python if, elif and elseAn if statement lets a program make a decision: it checks a condition and runs a block of code only when that…
  2. 2Python Logical ConditionsA logical condition joins several True/False tests with and, or and not, so a single if statement can weigh m…
  3. 3Python Nested IfA nested if is an if statement placed inside another if (or else) block, so a second question is only asked a…
  4. 4Python Shorthand If (Ternary)Shorthand if gives you two compact forms: a one-line if statement for a single action, and the conditional ex…
  5. 5Python Truthy and Falsy ValuesEvery Python value can be used as a condition: a short list of values (zero, empty text, empty collections, N…
  6. 6Python pass Statementpass is a statement that does nothing; it exists because Python blocks cannot be empty, so you use it to hold…
  7. 7Python match StatementA match statement compares one value against a series of patterns, runs the first case that fits, and can pul…
  8. 8Python for LoopsA for loop runs the same block of code once for every item in a collection, giving the current item a name wh…
  9. 9Python while LoopsA while loop keeps repeating a block of code for as long as its condition is True, checking the condition aga…
  10. 10Python range() Functionrange() produces a lazy sequence of whole numbers described by a start, a stop that is never included, and a …
  11. 11Python break and continuebreak ends a loop immediately, while continue abandons only the current round and jumps ahead to the next one.
  12. 12Python else on LoopsThe else block of a for or while loop runs once when the loop finishes normally, and is skipped when a break …
  13. 13Python Nested LoopsA nested loop is a loop written inside the body of another loop, so the inner loop starts over and runs compl…
  14. 14Python enumerate() and zip()enumerate() gives you each item together with its position, and zip() walks two or more sequences side by sid…

Start with Python if, elif and else →

About Control Flow

Control flow decides which lines run and how often. You will learn if, elif and else, combining conditions with and, or and not, truthy and falsy values, the match statement, and the for and while loops with range, break, continue and loop-else. These tools turn a list of instructions into a program that reacts to data. Practise nested loops with number tables and patterns, and compare your solutions with the worked programs linked below.

Related reference: Python programs · Control flow cheat sheet · break vs continue vs pass

Further reading (official documentation)

Practice for this topic