Conditions
- if, elif, else. Python chains decisions with if, elif and else: the first true branch runs and the rest are skipped.
- Comparisons and membership. Beyond == and <, the in operator asks whether a value sits inside a string, list or dictionary.
- Truthiness. Empty things are false in a condition: an empty string, list or dict all count as falsy.
Iteration and Loops
- The for loop. A Python for loop walks the items of any iterable directly: no index bookkeeping unless you ask for it.
- range and enumerate. enumerate hands you each item together with its position, replacing the manual counter.
- while and break. A while loop runs until its condition fails, and break exits any loop the moment you are done.
Writing Clear Program Flow
- Guard clauses. Handling the bad case first and returning early keeps functions flat: a guard clause beats nested ifs.
- Loop else. Python’s loop else runs only when the loop finished without break: a niche tool worth recognising when read.
- Flat is better. The Zen of Python says flat is better than nested: every extra level of indentation is a tax on the reader.
Before and after this level
- Before: Level 1: Python Foundations: Variables, Values and Operators, Strings, Numbers and Booleans, Reading and Writing Python.
- Next: Level 3: Python Collections: Lists and Tuples, Dictionaries and Sets, Comprehensions and Collection Patterns.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Python Level 2: Decisions & Loops cover?
Conditions, Iteration and Loops, Writing Clear Program Flow: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 1: Python Foundations, which covers Variables, Values and Operators, Strings, Numbers and Booleans, Reading and Writing Python.