Gulberg Green, Islamabad

Programming Fundamentals · Lesson 05

Conditions and decisions

Use a true-or-false question to choose which instructions run.

A condition is an expression that evaluates to True or False. Compare two values with operators such as == for equality, < for less than, or >= for greater than or equal to.

An if statement checks a condition. When the condition is true, Python runs the indented lines beneath it. An else clause supplies a second path when the condition is false. End each branch heading with a colon and indent its instructions consistently.

Think of a workshop that has space for twelve people. A check before accepting another visitor keeps the group within that limit.

Read the example

Python 3
registered = 11
capacity = 12
if registered < capacity:
    print("A seat is available.")
else:
    print("The workshop is full.")
Expected output
A seat is available.

Eleven is less than twelve, so Python runs the first print call. Set registered to 12 and it will take the else branch. Testing the exact boundary catches an easy mistake.

Try it yourself

Write a condition that prints “Ready” when a battery level is at least 20, and “Charge first” otherwise. Test it at 19 and at 20.

Show a worked solution
battery = 20
if battery >= 20:
    print("Ready")
else:
    print("Charge first")

Check your understanding

Your lesson quiz

Answer all five questions. Score at least 4 out of 5 (80%) to pass. You can retry as often as you like.

1. Which operator checks equality in Python?
2. Is 12 < 12 true or false?
3. Which condition means the battery is at least 20?
4. When does the else branch run?
5. How does Python identify the instructions inside an if branch?

Quiz progress is saved in this browser. View certificates →

Learn together

Was this lesson helpful?

Comments and reactions are not open yet.

WhatsApp