Conditionals: if / elif / else
Selection
Part of: Algorithms, Iteration & Simulation
A program that always does the same thing is just a calculator. Conditionals let code choose a path based on a Boolean condition, this is called selection , one of the three pillars of Big Idea 3 alongside sequencing and iteration. The if statement Python evaluates the condition temp 30. If it is True , the indented block runs; if False , the block is skipped entirely. Indentation is how Python knows what belongs inside the if. if / else: two paths When you want one action for True and a different action for False, add else: Exactly one branch runs, never both, never neither. elif: many paths For several mutually exclusive cases, chain elif ("else if"). Python checks each condition top to bottom and runs the first one that is True, then skips the rest: A score of 95 matches the first test and prints A. A score of 82 fails the first test, passes score = 80, prints B, and the remaining branches are never examined. Order matters : if you wrote score = 70 first, every passing grade would print C. The AP pseudocode form The exam writes this as IF (condition) { ... } ELSE { ... }. The block structure is the same; Python just uses indentation instead of braces. Why it matters Selection is
Challenge: Letter Grade