Conditions
Learn how to use if, elif, and else statements to make decisions in your code based on logical evaluations.
Learning Goals
The Core Concept
Conditional statements let your code make decisions on the fly. By using if, elif (else if), and else statements, you can choose which blocks of code execute based on whether a condition evaluates to True or False.
Python uses indentation (four spaces or a tab) to define which code belongs inside a condition block. This is a critical feature: if you do not indent your code correctly, Python will either run it in the wrong order or throw an IndentationError. We use comparison operators like == (equal to), != (not equal to), <, and > to construct conditional statements.
Visual guide
Python automation process
A simple original diagram to connect the lesson idea with real project flow.
Code & Implementation
temperature = 22
if temperature > 30:
print("It's a hot day!")
elif temperature >= 15:
print("It's a nice, warm day.")
else:
print("It's cold outside, grab a jacket.")Expected Output
It's a nice, warm day.
Automatic Gatekeeper
Hands-on practice task
The Challenge
Write a script that checks a user's age and subscription status. If they are 18+ and active, grant full access. If they are 18+ but inactive, offer a renewal page. Otherwise, deny access.
Helpful Hints
- •Set variables age = 20 and is_active = False.
- •Use logical operator 'and' or nested conditions.
Quick Knowledge Check
What is the difference between = and ==?
Can I nest if statements inside other if statements?
Continue Learning
Next steps after this lesson
Write a script that checks a user's age and subscription status. If they are 18+ and active, grant full access. If they are 18+ but inactive, offer a renewal page. Otherwise, deny access.
Supercharge your career workflows!
Discover free online utilities to format data, build job applications, and automate your productivity routine with TechIdea.