One-sentence summary
Conditions are the structures that let a program make decisions by saying, “if this is true, do this; otherwise, do something else.”
Why does it matter?
The programs we have written so far ran from top to bottom along a single path. But real programs make decisions all the time. A game prints “Game over” when your lives run out. A quiz app shows a different message depending on your score. A robot stops when there is an obstacle close in front of it.
In the Scratch module we used the “if / else” blocks. Python has the same idea; we just use words and indentation instead of coloured blocks. By the end of this lesson you will be able to make a program behave differently depending on the data it receives.
if / else and blocks with indentation
In Python a decision starts with the word if. After if we write a condition; if the condition is true (True), the lines below it run.
A simple example:
age = 15
if age >= 13:
print("You can see this content.")
Notice two things here:
- The condition is followed by a colon (
:). - The next line is indented by 4 spaces. This shifting inward is called indentation. It shows that those lines belong to the
ifblock.
Indentation is not decoration in Python; it defines where a block begins and ends. Indented lines are inside the condition; lines back at the left margin are outside it.
If the condition is false (False) and we want to do something else, we add else:
age = 10
if age >= 13:
print("You can see this content.")
else:
print("You need to be a little older for this content.")
This program prints only one message: the first if the condition is true, the second if it is false.
Comparison operators
A condition usually compares two values. For this we use comparison operators. Each one produces either True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | is equal to | 5 == 5 | True |
!= | is not equal to | 5 != 3 | True |
< | is less than | 3 < 5 | True |
> | is greater than | 3 > 5 | False |
<= | is less than or equal to | 5 <= 5 | True |
>= | is greater than or equal to | 4 >= 5 | False |
The most common mix-up here is this: a single = is used to assign a value, while a double == is used to compare two values.
age = 12 # assignment: put 12 into the variable age
print(age == 12) # comparison: is age equal to 12? -> True
print(age != 10) # True
print(age < 18) # True
print(age >= 13) # False
Example: Odd or even?
We find out whether a number is even with the % (modulo, remainder) operator. % gives the remainder of a division. If a number divides evenly by 2 (remainder 0), it is even.
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
For example, if you enter 8, 8 % 2 is 0, the condition is true, and “Even number” is printed. If you enter 7, 7 % 2 is 1, the condition is false, and the else runs.
elif for more than two paths
Sometimes there are not two but three or more paths. With elif (else if) we can check several conditions in order.
Python tries the conditions from top to bottom. The block of the first condition that is true runs, and the rest are skipped.
Example: Evaluating an exam score.
score = int(input("Enter the exam score: "))
if score >= 90:
print("Excellent")
elif score >= 70:
print("Good")
else:
print("There is an area you can improve")
Order matters here. If you enter 95, the first condition (>= 90) is true and “Excellent” is printed. If you enter 75, the first condition is false, elif score >= 70 is true, and “Good” is printed. If you enter 40, none is true, so the else runs.
Notice that we listed the conditions from high to low. If we had put elif score >= 70 at the top, high scores like 95 would also become “Good,” because 95 is already greater than 70 and Python stops at the first true condition.
Logical operators: and / or / not
Sometimes a decision depends not on one condition but on several at once. To combine them we use logical operators.
and: The result isTrueonly if both conditions are true.or: The result isTrueif at least one condition is true.not: Turns true into false and false into true.
age = 15
has_permission = True
if age >= 13 and has_permission:
print("You can join the event.")
if not has_permission:
print("You need to get permission first.")
The first condition requires both that the age is 13 or over and that permission exists; both must be true. or offers a choice instead:
weather = "rainy"
if weather == "rainy" or weather == "snowy":
print("Take an umbrella with you.")
Here the message is printed if the weather is “rainy” or “snowy”; either one is enough.
Mini practice
Write a program that decides a cinema ticket type based on age. The rules:
- If the age is under 12: print “Child ticket”.
- If the age is 65 or over: print “Senior ticket”.
- In all other cases: print “Standard ticket”.
Template:
age = int(input("Enter your age: "))
if age < 12:
print("Child ticket")
elif age >= 65:
print("Senior ticket")
else:
print("Standard ticket")
Run the program and try different values such as 8, 40, and 70. Then add one more rule yourself: what should happen when the age is exactly 12? Check what the code prints in that case and adjust the logic the way you want.
Common mistakes
Using = when comparing
Wrong: if age = 12: This raises an error, because a single = is assignment. The correct form is if age == 12:.
Forgetting the colon
If you leave out the : at the end of if score >= 90, Python raises a SyntaxError. Every if, elif, and else line ends with a colon.
Mixing up indentation
Lines inside a block must be indented by the same amount. If you write one line with 4 spaces and another with 3, you get an IndentationError. Always use 4 spaces and stay consistent.
Ordering elif incorrectly
If you write conditions in the wrong order, even a high score can fall into a lower branch. Usually the narrowest or highest condition should go at the top.
Safety note
While learning to code, only run code that you wrote yourself or that comes from a source you trust. Do not run unfamiliar code you found online without understanding it. Also, when a program asks for information through input, do not type personal data such as a real address or password; numbers and made-up values are enough for practice.
Lesson summary
ifsets up a condition; if it isTrue, the indented block below it runs.elseruns when the condition is false;elifchecks several paths in order.- Comparison operators (
==,!=,<,>,<=,>=) produceTrueorFalse. - Indentation defines the boundary of a block in Python; use 4 spaces and be consistent.
- With
and,or, andnotyou can combine conditions and build smarter decisions.
Check questions
- What value must a condition produce for the lines inside its
ifblock to run? - What is the difference between
=and==? - When
score = 75, what does the following code print?
if score >= 90:
print("Excellent")
elif score >= 70:
print("Good")
else:
print("There is an area you can improve")
- What does the condition
number % 2 == 0check? - What must be true for
age >= 13 and has_permissionto beTrue?
Answers
- The condition must produce
True. If it isFalse, the block is skipped. - A single
=assigns a value to a variable, while a double==compares whether two values are equal. - It prints “Good”. The first condition (
75 >= 90) is false, the second (75 >= 70) is true, and Python stops there. - It checks whether the remainder of dividing the number by 2 is 0, that is, whether the number is even.
- The value of
agemust be 13 or greater andhas_permissionmust beTrue. Withand, both conditions must hold.
Source and verification note
For “Conditions in Python”, verification focuses on whether the relationship between if / else and blocks with indentation and Example: Odd or even? remains consistent across examples. Code examples follow Python 3 syntax. Small differences may appear between environments, so examples should first be tested in a safe online editor or a local development setup.
Next lesson
For and While Loops: We will learn how to tell a program how many times to do a job, instead of writing the same steps over and over by hand.