🧠 Python Control Statements: Making Decisions in Python; The Power of Control Statements
Imagine strolling through a deep, quiet forest. You come to a fork in the road — one path to the left, another to the right. You pause, check your map, and decide. That simple moment of choice is exactly what happens in your Python programs every day.
In programming, these decision points are handled by control statements — they help your code decide what to do next depending on certain conditions.
Let’s explore how Python does this using booleans, if statements, and a few other friends.
🧭 Booleans: True or False, the Language of Decisions
In Python, a boolean (named after mathematician George Boole) represents one of two states:
True
False
These values act as the brains of decision-making. They help determine which road your program will take next.
Here’s a small example:
condition = True
if condition == True:
print("The condition")
print("was true")
Python checks whether condition
equals True
.
If yes, the two indented lines are executed. The indentation (the 4 spaces) tells Python:
“Hey, these lines belong together as one block!”
Now let’s expand it slightly:
condition = True
if condition == True:
print("The condition")
print("was true")
print("Outside of the if")
Here’s what happens:
- The first two
print()
lines run only ifcondition
isTrue
. - The last
print()
always runs — because it’s outside the indented block.
Python reads indentation the way humans read paragraphs — it groups related ideas.
⚖️ else
: The Other Path
Sometimes your condition isn’t true. Python lets you handle that too — with the else
statement.
condition = False
if condition == True:
print("The condition")
print("was True")
else:
print("The condition")
print("was False")
When condition
is False
, the if
part is skipped, and the else
part runs instead.
You can think of else
as the “otherwise” road.
🔀 elif
: Choosing Among Many Roads
Life isn’t always just yes or no. Sometimes you’ve got multiple choices.
That’s when Python’s elif
(short for else if) steps in.
condition = False
name = "Roger"
if condition == True:
print("The condition was True")
elif name == "Roger":
print("Hello Roger")
elif name == "Syd":
print("Hello Syd")
elif name == "Flavio":
print("Hello Flavio")
else:
print("The condition was False")
How this works:
- Python checks the first condition.
- If it’s false, it checks the next
elif
condition. - It keeps checking each in order.
- If none match, it finally executes the
else
block.
This gives your code a clear “if → elif → else” flow, just like a decision tree.
🧠 Shortcut Thinking: The Ternary Operator
Sometimes, decisions are short and sweet — like saying,
“If it rains, take an umbrella. Otherwise, wear sunglasses.”
Python lets you write these compact, one-line decisions using a ternary operator:
a = 2
result = 2 if a == 0 else 3
print(result) # Output: 3
You read this as:
“Set
result
to 2 ifa == 0
, otherwise set it to 3.”
It’s elegant, quick, and perfect for assigning values based on conditions.
🧩 Mini Challenges (Try These Out!)
- Write a program that checks whether a number is positive, negative, or zero.
- Ask the user for their age. If it’s below 18, print
"Minor"
, else print"Adult"
. - Write an
if...elif...else
block that greets a person if their name is Alice, Bob, or Charlie, and says"Who are you?"
otherwise. - Use a one-line if…else to assign
"even"
or"odd"
based on whether a number is even or odd. -
Create a simple password check:
- If the password is
"python123"
, print"Access granted"
. - Otherwise, print
"Access denied"
.
- If the password is
💡 Final Thoughts: Control = Intelligence
Control statements are the heartbeat of logic in programming. They give your programs the power to think, respond, and adapt — just like you choosing your path in that forest.
Every if
, elif
, and else
you write makes your code more aware and intelligent.
Keep practicing, and soon, you’ll guide your programs confidently through every forest of logic. 🌲🐍💻
🎯 Review Fill-in-the-Gap Questions
- A ___ value in Python can be either
True
orFalse
. - Code inside an
if
statement must be ___ to show it belongs to the block. - The
else
statement runs only when theif
condition is ___. - To handle multiple conditions, we use the keyword ___.
- The term
elif
stands for ___. - The short one-line conditional form in Python is called the ___ operator.
result = "Yes" if answer == "Y" else "No"
is an example of a ___ decision.- In Python, blocks of code are grouped using ___ instead of braces
{}
. - The
if...elif...else
structure works like a ___ of decisions. - Control statements allow a program to make ___ based on conditions.