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 if condition is True.
  • 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:

  1. Python checks the first condition.
  2. If it’s false, it checks the next elif condition.
  3. It keeps checking each in order.
  4. 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 if a == 0, otherwise set it to 3.”

It’s elegant, quick, and perfect for assigning values based on conditions.


🧩 Mini Challenges (Try These Out!)

  1. Write a program that checks whether a number is positive, negative, or zero.
  2. Ask the user for their age. If it’s below 18, print "Minor", else print "Adult".
  3. Write an if...elif...else block that greets a person if their name is Alice, Bob, or Charlie, and says "Who are you?" otherwise.
  4. Use a one-line if…else to assign "even" or "odd" based on whether a number is even or odd.
  5. Create a simple password check:

    • If the password is "python123", print "Access granted".
    • Otherwise, print "Access denied".

💡 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

  1. A ___ value in Python can be either True or False.
  2. Code inside an if statement must be ___ to show it belongs to the block.
  3. The else statement runs only when the if condition is ___.
  4. To handle multiple conditions, we use the keyword ___.
  5. The term elif stands for ___.
  6. The short one-line conditional form in Python is called the ___ operator.
  7. result = "Yes" if answer == "Y" else "No" is an example of a ___ decision.
  8. In Python, blocks of code are grouped using ___ instead of braces {}.
  9. The if...elif...else structure works like a ___ of decisions.
  10. Control statements allow a program to make ___ based on conditions.

<
Previous Post
🌄 How to Set an Image as the Background of an HTML Page (Step-by-Step)
>
Next Post
🐍 Python Lists: Your Digital Shopping Basket