Conditionals: if / elif / else
You'll learn to
- -Write if / elif / else chains to branch program logic
- -Combine conditions with and, or, and not
- -Nest conditionals when a decision depends on more than one level of logic
- -Know that Python has no switch statement, and what replaces it
Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off, saved just for you on this device.
So far every snippet in this course has run top to bottom, every line, every time. Real programs need to make decisions: do this if a condition holds, do something else if it does not. That is what conditionals are for.
Comparison Operators
- -== means equal to (note two equals signs; one = is assignment, a very common typo bug).
- -!= means not equal to.
- -< and > mean less than and greater than.
- -<= and >= mean less than or equal to and greater than or equal to.
if / elif / else
Python evaluates conditions top to bottom and runs the first branch that matches, then skips the rest entirely, even if a later condition would also be true. elif (short for "else if") lets you chain as many conditions as you need. else is optional and catches anything not matched above. Note that Python uses indentation, not braces, to mark what belongs inside each branch. Four spaces is the near-universal convention, and getting the indentation wrong is a real syntax error, not just a style nitpick.
Combining Conditions: and, or, not
and requires both sides to be true, or requires at least one, and not flips a condition's truth value. Unlike some languages, Python spells these out as words rather than symbols like && or ||, one of many small ways Python favors reading like plain English.
Nesting Conditionals
Nesting is what you reach for when a decision genuinely depends on the outcome of a previous decision. It is powerful, but it is also the fastest way to make code hard to read. Three or four levels deep and a reader has to hold a lot of context in their head at once. Often, combining conditions with and (like age >= 18 and has_ticket) reads more clearly than nesting two separate if statements, when the logic allows it.
No switch Statement (and match/case, Briefly)
Unlike languages with a dedicated switch statement, Python has traditionally handled "check this value against several options" purely with if / elif / else chains, and that is exactly what you should reach for as a beginner. Modern Python (3.10+) does add a match/case statement for this exact pattern, worth knowing exists even though this course will not lean on it.
match/case is genuinely useful once your conditions get complex, especially matching on the shape of data, but if / elif / else handles everything you will need for the rest of this course. Do not feel behind for sticking with it.
You write a chain of separate if statements instead of if/elif/else to check age ranges, and someone points out a bug: more than one message prints for the same input. What happened?
"Probably a typo in one of the conditions."
"Separate if statements are each evaluated independently, so if more than one condition happens to be true, more than one branch runs. An elif chain fixes this by only checking the next condition when the previous one was false, and stopping entirely at the first match. If exactly one branch should run, the fix is switching from a stack of if statements to if/elif/else, not patching each individual condition one at a time."
In an if / elif / else chain, what happens once Python finds a branch whose condition is true?