๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ Python

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`.

5 min 10 XP Lesson 4 of 21
Conditions (if/else)
๐ŸŒ

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.

  • โ€ขif runs a block only when its condition is True
  • โ€ขelif (else-if) tries another condition when the first was False
  • โ€ขelse catches 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 True or False
  • โ€ขStep 2: If True, the indented code block runs
  • โ€ขStep 3: If False, Python jumps to elif (if there is one) or else
  • โ€ข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) or or (at least one must be True)
  • โ€ขnot flips a boolean: not True is False
  • โ€ขYou can nest if statements inside other if statements (called nested conditions)
  • โ€ขThe : colon at the end of if, elif, else is required โ€” don't forget it
  • โ€ขYou can have many elif branches but only one if and one else
โš ๏ธ

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) or or (at least one must be True)
  • โ†’not flips a boolean: not True is False
  • โ†’You can nest if statements inside other if statements (called nested conditions)
  • โ†’The : colon at the end of if, elif, else is required โ€” don't forget it
  • โ†’You can have many elif branches but only one if and one else

Real-World Examples

โ€ข A game: <code>if health &lt;= 0: show_game_over()</code> โ€ข A login: <code>if password == stored_password: grant_access() else: deny()</code> โ€ข YouTube age restriction: <code>if age &lt; 13: block_content()</code> โ€ข A thermostat: <code>if temperature &gt; target: turn_on_ac() elif temperature &lt; target: turn_on_heat()</code> โ€ข Spotify shuffle: <code>if shuffle_on: pick_random_track() else: play_next_track()</code>

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

1 / 2

When does the else block run?