L2·Control & Collections

🔀if / elif / else

Branching on conditions.

ifelifelsematch

§ 1Basic branching

Python uses indentation to define blocks — no braces.

§ 2Ternary expression

value_if_true if condition else value_if_false

§ 3match/case (3.10+)

Pattern matching for structural dispatch. Not a switch — patterns bind names and destructure.

Worked example

1score = 87​
2if score >= 90:​
3 grade = 'A'​
4elif score >= 80:​
5 grade = 'B'​
6else:​
7 grade = 'C'​
8print(grade)​

Try it live · Python runs in your browser

Loading playground…

Checkpoint quiz

Question 1 / 2
How do you write a one-line conditional?

Lesson test

Take an 8-question test on “if / elif / else” — concepts, output prediction, bug spotting and interview questions.