if / else
Until now, every line of your code ran in order, top to bottom. That changes right now. With if and else, your programs learn to choose.
if statementelse for the alternate path%Your First if Statement
An if statement checks a condition. If it's True, the indented code runs. If it's False, Python skips it entirely.
Let's break down the syntax โ there are three crucial parts:
if โ tells Python a decision is coming.2. The condition โ any expression that evaluates to True or False (like the ones from Lesson 2.1!).
3. The colon
: โ don't forget it! This marks the start of the indented block.
The Power of Indentation
Python uses indentation (4 spaces) to know which lines belong to the if block. Everything indented runs only when the condition is True:
Adding else
What if you want to do something different when the condition is False? That's what else is for:
Think of it like a fork in the road โ your code always takes one path or the other, never both:
Storing Results from Branches
A common pattern is to set a variable inside the branches:
This works because exactly one branch always runs, so result is always defined by the time we reach the print().
The Modulo Trick
Remember the % operator from Unit 1? It returns the remainder after division. Combined with if, it's incredibly useful:
Challenges
Challenge 1: The Bouncer
You're the bouncer at a club. Check the patron's age and set the right message.
โข If age is 21 or older โ set message to "Welcome in!"
โข Otherwise โ set message to "Come back when you're 21."
if age >= 21: then indent the next line to set message.Hint 2: Don't forget the
else: on its own line, followed by an indented line setting the other message.Hint 3:
if age >= 21:message = "Welcome in!"else:message = "Come back when you're 21."
Challenge 2: Even or Odd
Use the modulo operator to classify a number as even or odd.
โข If number is even โ set parity to "even"
โข Otherwise โ set parity to "odd"
number % 2 == 0. That's your condition!Hint 2:
if number % 2 == 0: then set parity to "even" in the indented block.
Challenge 3: Pass or Fail
Determine if a student passed or failed based on their score, and build a result message.
โข If score is 60 or higher โ set result to "Pass"
โข Otherwise โ set result to "Fail"
โข Then create report as an f-string: "Score: 73 โ Pass" (using the actual score and result)
result. Then on a new line (not indented inside either block), create the report.Hint 2: The report format is:
f"Score: {score} โ {result}". Note the em-dash (โ), not a hyphen.Hint 3: Make sure the report line is outside the if/else, at the same indentation level as the
if keyword.
Challenge 4: Build Your Own abs()
Python has a built-in abs() function, but can you build one yourself with just if/else?
โข If the number is negative โ set absolute to number * -1
โข Otherwise โ set absolute to number as-is
Do NOT use the built-in abs() function! The point is to build the logic yourself.
number < 0. To make it positive, multiply by -1.Hint 2: If the number is already positive (or zero), you don't need to change it at all.
Hint 3:
if number < 0:absolute = number * -1else:absolute = number