Back to the main roadmap | Previous lesson: Variables and Data Types | Next lesson: Type Casting
Conditionals allow your program to make decisions.
Think of it like giving instructions to a robot:
If something is true, do one thing. Otherwise, do something else.
If it's raining, take an umbrella. Otherwise, don't.
Python uses three keywords to make decisions:
ifelifelse
weather = "rainy"
if weather == "rainy":
print("Take an umbrella")
elif weather == "sunny":
print("Wear sunglasses")
else:
print("Just go outside normally")Take an umbrella
Python checks each condition from top to bottom.
if→ Checks the first condition.elif→ Checks another condition only if the previous one wasFalse.else→ Runs if none of the previous conditions wereTrue.
Python stops checking as soon as it finds the first True condition.
This is one of the most common beginner mistakes.
| Symbol | Meaning |
|---|---|
= |
Assigns a value to a variable |
== |
Compares two values |
age = 10This stores the value 10 in the variable age.
age = 10
print(age == 10)
print(age == 5)True
False
age == 10asks: "Is age equal to 10?"age == 5asks: "Is age equal to 5?"
The answer is either True or False.
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
x = 10
print(x > 5)
print(x < 5)
print(x != 8)True
False
True
Sometimes you need to check more than one condition.
Python provides three logical operators:
andornot
Both conditions must be True.
age = 20
has_id = True
if age >= 18 and has_id:
print("You can enter")
else:
print("You cannot enter")You can enter
At least one condition must be True.
is_weekend = False
is_holiday = True
if is_weekend or is_holiday:
print("No school today")No school today
Reverses a Boolean value.
is_logged_in = False
if not is_logged_in:
print("Please log in")Please log in
Create a file named lesson3.py and write the following code.
age = 15
has_permission = False
if age >= 18:
print("You are an adult")
elif age >= 13 and age < 18:
print("You are a teenager")
else:
print("You are a kid")
if age >= 13 or has_permission:
print("You can watch the movie")
else:
print("Sorry, no movie for you")You are a teenager
You can watch the movie
ageis 15, so it is not greater than or equal to 18.- It is greater than or equal to 13, so Python prints "You are a teenager."
- The second condition uses
or. - Since
age >= 13is alreadyTrue, Python prints "You can watch the movie." - It doesn't matter that
has_permissionisFalsebecauseoronly needs one condition to beTrue.
Change the values:
age = 20
has_permission = TrueThe output becomes:
You are an adult
You can watch the movie
Why?
age >= 18is nowTrue, so Python prints "You are an adult."- Both
age >= 13andhas_permissionareTrue, so the second message remains "You can watch the movie."
- Conditionals allow programs to make decisions.
ifchecks the first condition.elifchecks another condition if the previous one wasFalse.elseruns when no conditions areTrue.=assigns a value, while==compares values.- Use comparison operators to ask questions about values.
andrequires all conditions to beTrue.orrequires at least one condition to beTrue.notreverses a Boolean value.
Back to the main roadmap | Previous lesson: Variables and Data Types | Next lesson: Type Casting
Next Lesson: Type Casting