Conditions (if/else)
๐ What are conditions? Conditions let your program **make decisions** instead of always doing the same thing. You ask a yes/no question in code; if the answer is true, one block runs; if not, you can run a different block with `else` or chain extra checks with `elif`.

Appy Saysโฆ
Every app you use is full of decisions. Is the user logged in? Is the score high enough? Does the password match? If/else is how code makes choices โ without it, every program would just do the exact same thing every single time.
What are Conditions?
Conditions let your program make decisions. You ask a yes/no question in code โ if the answer is True, one block runs; if it's False, a different block runs. This is called branching.
- โข
ifruns a block only when its condition isTrue - โข
elif(else-if) tries another condition when the first wasFalse - โข
elsecatches every other case โ your safe default - โขIndentation (the 4-space gap) tells Python what belongs inside each branch
Think of it like a Roblox game checkpoint
When you reach a checkpoint, the game checks: 'Does the player have enough points to unlock the next zone?' If yes โ open the gate. If not โ show the 'you need more points' screen. If they've already been here โ skip the cutscene. That's if / elif / else in action.
How Conditions Work โ Step by Step
- โขStep 1: Python evaluates your condition โ the result is
TrueorFalse - โขStep 2: If
True, the indented code block runs - โขStep 3: If
False, Python jumps toelif(if there is one) orelse - โขStep 4: Only one branch runs โ once a match is found, the rest are skipped
- โขComparison operators:
==equals,!=not equal,>greater,<less,>=at least,<=at most
Real-World Examples
- โขA game:
if health <= 0: show_game_over() - โขA login:
if password == stored_password: grant_access() else: deny() - โขYouTube age restriction:
if age < 13: block_content() - โขA thermostat:
if temperature > target: turn_on_ac() elif temperature < target: turn_on_heat() - โขSpotify shuffle:
if shuffle_on: pick_random_track() else: play_next_track()
Key Facts
- โขCombine conditions with
and(both must be True) oror(at least one must be True) - โข
notflips a boolean:not TrueisFalse - โขYou can nest if statements inside other if statements (called nested conditions)
- โขThe
:colon at the end ofif,elif,elseis required โ don't forget it - โขYou can have many
elifbranches but only oneifand oneelse
Watch Out: = vs ==
= assigns a value (sets a variable). == compares two values. Writing if score = 10: is a common mistake โ it should be if score == 10:. Python will give you a SyntaxError to catch this.
Remember
Think of conditions as questions your code asks: 'Is this true right now?' Answer yes โ do this. Answer no โ do that. Break every complex decision into a chain of simple yes/no questions and you'll always be able to code it.
What You Learned
- โข
if condition:โ runs the block when condition is True - โข
elif condition:โ checks another condition if the first was False - โข
else:โ runs when no previous condition was True - โขComparison operators:
== != > < >= <= - โขCombine with
and/or/not - โขIndentation defines what belongs inside each branch
Key Facts
- โCombine conditions with
and(both must be True) oror(at least one must be True) - โ
notflips a boolean:not TrueisFalse - โYou can nest if statements inside other if statements (called nested conditions)
- โThe
:colon at the end ofif,elif,elseis required โ don't forget it - โYou can have many
elifbranches but only oneifand oneelse
Real-World Examples
Remember
Think of conditions as questions your code asks: 'Is this true right now?' Answer yes โ do this. Answer no โ do that. Break every complex decision into a chain of simple yes/no questions and you'll always be able to code it.
Quick Quiz
When does the else block run?