Understand Python Conditional Statements Using Everyday Scenarios


Have you ever considered how many decisions you make in a single day? From the moment your alarm rings in the morning, you are faced with choices—do you hit snooze for a few more minutes of rest, or do you jump out of bed to start your day? Each of these decisions, whether made consciously or subconsciously, plays a crucial role in shaping the flow of your daily life.
Similarly, in the world of programming, decision-making is equally important. Understanding the flow of logic within a program is crucial for developing efficient and effective software. Python, a widely used programming language, provides a set of conditional statements fundamental to this logical flow. These conditional statements, such as if
, elif
, and else
allow programmers to execute specific blocks of code based on certain conditions. By mastering these statements, developers can create programs that respond dynamically to different inputs and scenarios, making decision-making in programming as vital as it is in everyday life.
⏰ if
Statement
Imagine it's Sunday today. After a long and busy week filled with various tasks and responsibilities, you made a decision last night that today you will allow yourself the luxury of sleeping in and waking up later than usual.
If you're a programmer, you can turn this scenario into a fun coding task! Write a simple Python script using if
statement to display your choice to wake up late on a Sunday. The script checks the day; if it's Sunday, it runs code that outputs waking up late.
day = 'Sunday'
if day == 'Sunday': # if the given condition is true, then only print() works
print("Wake up Late.") # Wake up Late.
# another way
is_sunday = True
if is_sunday:
print("Wake up Late.") # Wake up Late.
Flowchart :
⏰ else
Statement
The else
statement in Python is a fundamental part of conditional logic, providing a way to handle situations where the initial condition is not met. Imagine you have set a routine for your week, where you allow yourself to sleep in only on Sundays. For every other day, you need to wake up early to manage your responsibilities, whether it's work, school, or other commitments.
Let's expand our previous example to include this scenario. You can write a Python script that checks the day of the week. If it's Sunday, the script will output that you can wake up late. However, if it's any other day, the script will use the else
statement to indicate that you need to wake up early.
day = 'Monday'
if day == 'Sunday':
print("Wake up Late.") # This message appears only if the day is Sunday.
else:
print("Wake up Early.") # This message appears for all other days.
Flowchart:
⏰ elif
Statement
The elif
statement in Python is important for checking multiple conditions one after another. It stands for "else if" and is useful when there are more than two possible outcomes. Imagine your weekly routine changes based on the day. For example, our meals change at different times of the day: in the morning, we have breakfast; in the afternoon, we have lunch; at night, we have dinner; and at other times, we have snacks.
If we were to convert this scenario into a Python script, it would look like this:
time_of_day = 'morning' # Could also be others.
if time_of_day == 'morning':
print("It's time for breakfast.")
elif time_of_day == 'afternoon':
print("It's time for lunch.")
elif time_of_day == 'evening':
print("It's time for dinner.")
else:
print("It's time for a snack.")
Flowchart:
⏰Nested conditionals
Nested conditionals in Python let you put one conditional statement inside another, allowing you to check several conditions in an organised way. This is especially helpful when you have a series of decisions that rely on each other.
For example, if today is Sunday and the weather is sunny, you might choose to visit the Victoria Memorial Hall with your loved one. This way, you can enjoy the nice weather and the historic site together. But if the weather is bad, like rainy or very windy, you could stay indoors. In that case, you might watch a movie or play games online, making sure you have a fun day no matter the weather. In another scenario, if it is not Sunday, you will follow your regular routine and go to the office as usual.
day = "Sunday" # Could also be another day like "Monday"
weather = "sunny" # Could also be others.
# Check if today is Sunday
if day == "Sunday":
if weather == "sunny":
print("You're going to visit Victoria Memorial Hall with your loved one.")
else:
print("You're staying indoors, maybe watching a movie or playing games online.")
else:
print("You're going to the office as usual.")
Flowchart:
Just like we make choices every day—from when to wake up to what to do on weekends—programming also involves making decisions using logic and conditions. Python's conditional statements (if
, elif
, else
, and nested conditionals) help developers write code that can handle different situations, making apps smarter and more adaptable. By learning these basics, you're not just picking up syntax—you're learning to think logically, solve problems well, and create programs that act like real-life decision-making. So, the next time you choose between sleeping in or going out early, remember: every if
has a purpose, and every else
takes you somewhere new. Keep coding and keep making choices!
Subscribe to my newsletter
Read articles from Nabin Garai directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
