Making decisions
Branch with if, elif and else, combine tests with and/or/not, and put conditions in the right order.
18 min
Programs become useful when they can choose. An if statement runs its indented block only when its condition is true:
temperature = 31
if temperature > 30:
print("Hot")
elif temperature > 15:
print("Mild")
else:
print("Cold")Python tests the conditions from the top and runs the first block that is true, then skips the rest. else catches everything left over.
Comparison operators give a bool: == (equal), !=, <, <=, >, >=. Note == compares; a single = stores.
Combine tests with and, or, not:
if age >= 18 and has_ticket:
print("Come in")The indented lines (four spaces) are the block. A missing colon or wrong indent is the most common beginner error in Python, and the message is IndentationError or SyntaxError.
An empty string, 0, 0.0, None and an empty list count as false in a condition; everything else counts as true. So if name: means "if name is not empty".
Python walks down the chain and stops at the first yes. A score of 74 fails the first two tests and lands on C.
You are writing the shipping rule for a small shop. Orders over $50 ship free, orders over $20 cost $5, and everything else costs $8. A teammate sends you their first attempt.
Their code starts with `if total > 20:` (cost 5), then `elif total > 50:` (free), then `else:` (cost 8). A $60 order is placed. What happens?
Write shipping(total, is_member) returning the shipping cost in dollars:
- members always pay
0 - a total of
50or more is0 - a total of
20or more is5 - anything else is
8
Your code must satisfy
- shipping(10, False)→8small order
- shipping(20, False)→5exactly 20
- shipping(49.99, False)→5just under 50
- shipping(50, False)→0exactly 50
- shipping(10, True)→0member
- shipping(0, False)→8zero
def shipping(total, is_member):
passThis function should turn a score into a letter: 90 and up is A, 80 and up is B, 70 and up is C, 60 and up is D, anything lower is F. Find the bug and fix it.
What it does now
letter_grade(95) returns "D" instead of "A". Every score of 60 or more comes back as a D.
Your code must satisfy
- letter_grade(95)→"A"95 is A
- letter_grade(85)→"B"85 is B
- letter_grade(70)→"C"70 is C
- letter_grade(65)→"D"65 is D
- letter_grade(59)→"F"59 is F
def letter_grade(score):
if score >= 60:
return "D"
elif score >= 70:
return "C"
elif score >= 80:
return "B"
elif score >= 90:
return "A"
else:
return "F"ifruns a block only when its condition is true;elifandelsehandle the other cases.- Python takes the first true branch and skips the rest, so the order of conditions matters.
==compares and=stores;and,or,notcombine tests.- Always test the boundary values: the exact numbers where the rule changes.
Trace these by hand.
x = 15. Which branch runs: if x > 20: A / elif x > 10: B / else: C?
- AA
- BB
- CC
- DB and C
Which condition is true when age = 18?
- A`age < 18`
- B`age > 18`
- C`age >= 18`
- D`age != 18`
What does not (3 > 2 and 1 > 2) evaluate to?
- ATrue
- BFalse
- C3
- DAn error
Which of these count as false in an if?
- A`0`
- B`""`
- C`"False"`
- D`[]`
To test whether two values are equal, use , not a single =.
Capitalisation, spacing and punctuation are ignored when grading.
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of Python for Complete Beginners is planned around your week. Free.