By the end of this lesson, you will be able to:
:information_source: What are Conditional Statements? Conditional statements are rules that check conditions before executing or skipping blocks of code. They help your program make decisions!
Python usually follows a sequential flow. This means it reads and runs your code line by line, from top to bottom.
Sequential flow example:
┌─────────┐
( Start )
└────┬────┘
│
▼
┌─────────┐
│ a = 5 │
└────┬────┘
│
▼
┌─────────┐
│ b = 7 │
└────┬────┘
│
▼
┌─────────┐
│ c = a+b │
└────┬────┘
│
▼
┌─────────┐
│ print(c)│
└────┬────┘
│
▼
┌─────────┐
( End )
└─────────┘
Think about real life - you make decisions every day!
In programming, we need our code to make similar decisions. Conditional statements let our programs:
:bulb: Real-World Example Imagine a game where players must be at least 13 years old to play. Your program needs to check the player's age before letting them start!
Every conditional statement has two main parts:
A test that results in either True or False (called a Boolean expression)
The code that runs based on the condition:
Here's how an if statement looks:
if condition:
# This code runs if condition is True
:memo: Important! The colon (:) and indentation are required in Python. The indented code is the "code block" that runs when the condition is True.
To create conditions, we use two types of operators: Comparison and Logical.
Comparison operators compare two values and return True or False:
Operator | Meaning | Example | Result |
---|---|---|---|
== |
Is equal to | 2 == 3 |
False |
!= |
Not equal to | 2 != 3 |
True |
< |
Is less than | 2 < 3 |
True |
> |
Is greater than | 2 > 3 |
False |
<= |
Is less than or equal to | 2 <= 3 |
True |
>= |
Is greater than or equal to | 2 >= 3 |
False |
:warning: Common Mistake! Don't confuse
=
with==
:
=
assigns a value to a variable (likeage = 15
)==
compares two values (likeage == 15
)
Logical operators combine multiple conditions together:
Operator | Description | Example | Result |
---|---|---|---|
and |
Both conditions must be True | x = 10 x > 5 and x > 7 |
True |
or |
At least one condition must be True | x = 10 x > 5 or x < 7 |
True |
not |
Reverses the result | x = 10 not(x > 5) |
False |
:bulb: Remember!
and
: Both must be True :white_check_mark: AND :white_check_mark:or
: At least one must be True :white_check_mark: OR :x:not
: Flips the result :emoji:
Let's explore different types of conditional statements:
The simplest conditional statement. It runs code only when a condition is True.
if condition:
# Code runs if condition is True
age = 20
if age > 17:
print("You cannot join this class")
print("This class only for below 16 years old ")
Expected output:
You cannot join this class
This class only for below 16 years old
What happens:
age
is set to 20age > 17
? (Is `20 > 17`?):memo: Try It! Change
age
to 15 and run the code again. What happens? Nothing prints because 15 is not greater than 17!
When you have multiple conditions to check, use elif
(short for "else if").
if condition1:
# Runs if condition1 is True
elif condition2:
# Runs if condition1 is False AND condition2 is True
elif condition3:
# Runs if both above are False AND condition3 is True
else:
# Runs if all conditions are False
age = 15
if age == 13:
print("You are 13 years old")
elif age == 14:
print("You are 14 years old")
elif age == 15:
print("You are 15 years old")
elif age == 16:
print("You are 16 years old")
else:
print("Please enter age in range 13 - 16")
Expected output:
You are 15 years old
How it works:
else
block runsIn this example:
Use logical operators (and
, or
) to create more complex conditions.
age = 17
if age >= 10 and age <= 12:
print("You are the youngest student")
elif age >= 13 and age <= 15:
print("You are in the middle")
elif age >= 16 and age <= 17:
print("You are the senior in school")
else:
print("Please insert the correct age")
Expected output:
You are the senior in school
How the and
operator works:
age >= 16 and age <= 17
to be True:
:bulb: Range Checking Using
and
is perfect for checking if a value is within a range:
age >= 10 and age <= 12
-> Is age between 10 and 12?score >= 80 and score <= 100
-> Is score between 80 and 100?
==
, !=
, <
, >
, <=
, >=
) compare valuesand
, or
, not
) combine conditions:
after conditionsCode with AI: Try using AI to work with conditional statements.
Prompts:
Write a program that:
Create a program that:
Build a program that: