Home · Academy · Robotics & Coding · Python Fundamentals · Conditions in Python

Conditions in Python

Learn to make decisions in Python with if / elif / else and logical operators.

LESSON COMPASS

What will you use this page for?

Core idea

Conditions are the structures that let a program make decisions by saying, “if this is true, do this; otherwise, do something else.”

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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…

Next connection

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.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteGetting Input from the User
ContentStandard lesson · 1,432 words
Last updated

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:

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.

Comparison operators table
OperatorMeaningExampleResult
==is equal to5 == 5True
!=is not equal to5 != 3True
<is less than3 < 5True
>is greater than3 > 5False
<=is less than or equal to5 <= 5True
>=is greater than or equal to4 >= 5False

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.

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:

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

Check questions

  1. What value must a condition produce for the lines inside its if block to run?
  2. What is the difference between = and ==?
  3. 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")
  1. What does the condition number % 2 == 0 check?
  2. What must be true for age >= 13 and has_permission to be True?

Answers

  1. The condition must produce True. If it is False, the block is skipped.
  2. A single = assigns a value to a variable, while a double == compares whether two values are equal.
  3. It prints “Good”. The first condition (75 >= 90) is false, the second (75 >= 70) is true, and Python stops there.
  4. It checks whether the remainder of dividing the number by 2 is 0, that is, whether the number is even.
  5. The value of age must be 13 or greater and has_permission must be True. With and, 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.

Start QuizBack to Python Fundamentals
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.