โ† Back to Python Course
๐Ÿค” Making Decisionsยท20 minยทBeginner
๐Ÿ”€

If / Else

Teaching Python to choose

Learn how if and else let your program make decisions โ€” do this if something is true, otherwise do that.

๐Ÿ“–

What we are learning

The if statement is how Python makes decisions. You tell Python: "IF this is true, do this. ELSE, do that." It is like a fork in the road โ€” the program picks one path based on a condition.

๐Ÿ’ก Think of it like this:

Imagine you are walking home and see a sign: "IF it is raining, take the bus. ELSE, walk through the park." You check the weather and choose your path. Python does the same thing with if/else!

๐ŸŒŸ

Why it matters

Without if/else, every program would do the exact same thing every time. Decisions make programs smart โ€” they can react differently depending on the situation.

Where you see this in real life:

  • โœ“Games check if your health is zero to decide if you lost
  • โœ“Login screens check if your password is correct
  • โœ“Weather apps show different icons based on the temperature
๐ŸŽฏ

By the end, you can:

  • 1.Write an if statement
  • 2.Write an if/else statement
  • 3.Understand indentation in Python
๐Ÿ”ค

New words

ifelseelifconditionindentation
๐Ÿ
Think of a decision you make every day. What is the condition, and what do you do in each case?
๐Ÿค“

Fun Fact!

Python uses indentation (spaces at the start of a line) to know which code belongs to the if statement. Most other languages use curly braces { } instead!

๐Ÿ”€ Making Decisions with if/else

The ifstatement lets Python make decisions. It checks if something is true, and if it is, it runs some code. If not, it can run different code withelse.

โœจ Your First if Statement

Notice the indentation (spaces at the start of the line). Python uses indentation to know which code belongs to the if statement!

๐ŸTry if/else!
main.py
โ— Output
Change the age value and run again!

๐Ÿ’ก Try changing age to 8 and run again. What happens?

๐Ÿ›ค๏ธ The elif Keyword

What if there are more than two options? Useelif(short for "else if") to check multiple conditions!

๐ŸMultiple choices with elif
main.py
โ— Output
Click โ–ถ Run to see the output!

โš ๏ธ Indentation Matters!

In Python, indentation is not just for looks โ€” it is part of the syntax! Code inside an if statement MUST be indented (usually 4 spaces).

โœ… Correct

if True: print("Hi!") print("Bye!")

โŒ Error!

if True: print("Hi!") print("Bye!")
๐Ÿ
Try this: write an if/else that checks if a variable 'weather' is 'sunny'. If it is, print 'Go outside!' Otherwise print 'Stay inside!'
๐Ÿง 

Quick Quiz!

+10 XP

Why does Python use indentation (spaces at the start of lines)?

๐Ÿ
Finished the lesson? Click the button to mark it complete and earn XP!