Day 4 of 50 Days of Python: Control Flow with If, Else, and Elif
Part of week 1: Python Basics
Welcome to Day 4, hope you are enjoying the series so far! We’ve covered a fair amount of basics like variables, data types and the different types of operators. Today, we’re taking your Python skills to the next level by introducing you to control flow statements. These are the tools you’ll use to build logical decision making in your scripts and programs.
Control flow allows your program to evaluate conditions and execute specific blocks of code based on those conditions. Think of it as a sort of road with multiple paths to take, if you decide to take a path then you’ll experience different scenery (code).
What are Control Flow Statements?
Control flow in Python revolves around three main statements:
if
else
elif
If Statement
The if statement checks a condition. If the condition evaluates to True, the block of code within the if statement will be executed. see below:
temperature = 30
if temperature > 30:
print("It's hot today!")Else Statement
The else statement provides a fallback option. If the “if” condition is not met, the code inside the else block is executed
temperature = 30
if temperature > 30:
print("It's hot today!")
else:
print("it's cold today!")Elif Statement
The elif statement is short for “else if”. It’s used for checking additional conditions, and comes in handy when you need to evaluate different scenarios.
temperature = 20
if temperature > 30:
print("It's hot today!")
elif temperature > 15:
print("It's warm today!")
else:
print("it's cold today!")Comparison and Logical Operators Recap
Control flow works hand-in-hand with the operators that we covered in Day 3. Let’s recap:
Comparison Operators:
== (Equal to)
!= (Not equal to)
> (Greater than), < (Less than)
>=(Greater than or equal to), < (Less than or equal to)
Logical Operators:
and: Both conditions must be True.
or: At least one condition must be True.
not: Reverses a condition.
Nested If Statements
You can place if statements inside another to check more complex conditions:
age = 19
has_id = True
if age >= 18:
if has_id:
print("You are allowed to enter.")
else:
print("Please bring your ID to enter.")
else:
print("You are not old enough to enter.")Real-World Example: Data Engineer’s Perspective
Let’s say you’re validating incoming data in a pipeline. Based on the value you decide whether to process, discard, or raise an error.
record = {"user_id":"abc", "status":"invalid"}
if record["status"] == "valid":
print("Process the record.")
elif record["status"] == "invalid":
print("Discard the record.")
else:
print("Raise Error: Unknown Status.")Hands on Exercise
With what you’ve learnt today, and with the recap from yesterday’s coverage of operators, trying writing a grade checker that assigns a grade based on the below criteria:
100: A*
90: A
80-89: B
65-79: C
55-64: D
Below 55: F
Let me know in the comments how you get on and if you need any help. In two days I’ll leave the logic for this in the comments below as well.
Next Up: Day 5 - Loops: For & While
Tomorrow, we’re moving on to loops in Python. You’ll learn to automate repetitive tasks using for and while loops. These are game changers for any aspiring data engineers or Python developers!
Until then, happy coding and stay tuned.



