Conditions in Python (if, elif, else & Boolean Conditions)
1. What are Conditions in Python?
- Conditions help a program make decisions.
- They allow Python to choose what action to take based on different situations.
- Conditions work by checking whether something is True or False.
Example questions a program may ask:
- Is the user old enough?
- Does the user have a ticket?
- Is the game over?
2. What is an if Statement?
- An if statement runs a block of code only if a condition is true.
- If the condition is false, Python skips that block.
Example:
if condition:
# code runs here
- If the condition is True, the code inside runs.
- If the condition is False, the code does not run.
3. Using if, elif, and else
- Python checks conditions from top to bottom.
- Only one block runs — the first one that is true.
Keywords:
if→ checks the first conditionelif→ checks another condition if the previous one was falseelse→ runs when all conditions are false
Example idea:
- Check age
- If age is small → child
- Else if age is medium → teenager
- Else → adult
4. Conditions with Numbers
- Python can compare numbers using comparison operators:
- Greater than
> - Less than
< - Equal to
==
- Greater than
Example:
age = 18
if age >= 18:
print("You are allowed")
else:
print("You are not allowed")
Python checks the condition and decides which message to print.
5. What is a Boolean?
- A boolean is a data type that stores:
TrueFalse
- Booleans are used for yes or no situations.
Examples of boolean questions:
- Do you have a ticket?
- Is the user logged in?
- Is the light on?
- Is the game over?
6. Conditions Using Boolean Values
- When using booleans, we do not need comparison operators.
- Python directly checks whether the value is True or False.
Example:
has_ticket = True
if has_ticket:
print("You may enter, you have a ticket")
else:
print("You need to buy a ticket")
Explanation:
- If has_ticket is True → first message prints
- If has_ticket is False → else message prints
7. Why Boolean Conditions Are Important
Boolean conditions are used in:
- Login systems
- Payment checks
- Games (game over / game running)
- Apps and websites
- Button clicks
They help programs react automatically based on situations.
8. How Python Thinks
- Python checks conditions step by step
- It runs code only when conditions match
- This allows programs to behave intelligently
Conditions help your program:
- Think
- Decide
- React
9. Summary of What Students Learned
- What conditions are
- How if statements work
- How if, elif, else make decisions
- How conditions work with numbers
- What booleans are
- How to use boolean variables in conditions
10. Task for Students
Create boolean variables such as:
- is_raining
- is_student
- game_over
Use if and else to:
- Print different messages
- Decide what action should happen
Example idea:
- If it is raining → print “Take an umbrella”
- Else → print “Enjoy the weather”