PCC·05 · Chapter record
if Statements
Chapter 5 — teaching your programs to make decisions
Five sessions on branching logic: writing conditional tests, combining them with and/or/in, if/else, if-elif-else chains, and putting if statements to work inside loops over lists. Each one explains the concept, lists the key terms, then gives you exercises — try them yourself before revealing the code.
Equality & Comparison Tests
Concept
- At the heart of every if statement is a conditional test — an expression that evaluates to True or False.
==checks equality and!=checks inequality — a single=assigns a value, a double==asks a question.- Equality is case-sensitive:
'Audi' == 'audi'is False. Convert with.lower()before comparing if case shouldn't matter — it doesn't change the original variable. - The usual numeric comparisons work too:
< <= > >=.
Key Terms
- conditional test
- an expression that evaluates to True or False
- ==
- checks whether two values are equal
- .lower()
- converts a string to lowercase, for case-insensitive comparisons
Exercise
Write at least five conditional tests using ==, !=, <, <=, >, and >=. Predict True or False before you run each one.
Store a name with mixed capitalization, then write a test that compares it to a lowercase version using .lower().
Combining Conditions
Concept
andrequires both tests to be True for the overall expression to be True.orpasses if either test is True — it only fails when both tests fail.- Use
into check whether a value is inside a list —'mushrooms' in toppings. - Use not in to check that a value is missing from a list — handy for things like banned-user checks.
Key Terms
- and
- True only when both conditions are True
- or
- True when at least one condition is True
- in / not in
- checks whether a value is (or isn't) inside a list
Exercise
Pick two numbers and write both an and test and an or test that combine two comparisons on them.
Make a list of your favorite fruits, then write one test confirming a fruit is in the list and another confirming a fruit is not in the list.
if / else
Concept
if condition:runs the indented block only when the condition is True — if it's False, nothing happens.- Add else: to give Python something to do when the test fails — exactly one of the two blocks always runs.
- Just like for loops, indentation decides what belongs to each block.
Key Terms
- if
- runs a block only when its condition is True
- else
- runs when the if condition was False
Exercise
Store a color as a string. If it's "green", print one message; otherwise print a different message — using if/else.
Store an age. Using if/else, print "You can drive" if it's 16 or older, and how many years remain otherwise.
The if-elif-else Chain
Concept
elifadds another test that only runs if every test above it failed — Python checks top to bottom and stops at the first one that passes.elseat the end catches everything that matched none of the tests above — it's optional.- Chain as many
elifblocks as you need. - It's often cleaner to have the chain just set a variable, then print once after the chain, instead of repeating print() in every branch.
Key Terms
- elif
- an additional test, checked only if earlier tests failed
- chain
- an if / elif / … / else sequence — only one branch runs
Exercise
Give an alien a color — green, yellow, or red — and use if-elif-else to print how many points it's worth (5, 10, or 15).
Extend the admission-price example with a fourth bracket: seniors 65+ pay a discounted rate.
if Statements with Lists
Concept
- Put an
ifinside aforloop to treat one special item differently from the rest as you loop. - An empty list is falsy in an if test; a list with at least one item is truthy — handy for checking "is there anything here?" before looping.
- Check one list against another with
in— e.g. confirm each requested topping is actually available before adding it to the pizza.
Key Terms
- truthy / falsy
- a non-empty list is True in an if test; an empty list is False
Exercise
Check a requested username (case-insensitively) against a list of already-registered usernames. If it's taken, ask for a different one; if the registered list is empty, print a welcome message instead.