Blog Post #25: Making Decisions: Your First if Statement

For the last four posts, we have been assembling our logic toolkit: Booleans (Post #21), comparison operators (Post #22), logical operators (Post #23), and the concept of Truthy/Falsy values (Post #24). It is now time to use these tools to control the very flow of our programs.

In this post, you will learn how to write your first if statement. This is the fundamental building block of decision-making in Python, allowing your code to execute certain lines only when a specific condition is met.

The Fork in the Road

Imagine your program is a car driving down a road. So far, that road has been a straight line, executing code from top to bottom. An if statement is a fork in that road. The car reaches the fork and checks a sign (the condition). If the sign says ‘go this way’ (True), it turns down a special path. If the sign says ‘don’t go this way’ (False), it skips that path and continues straight on.

The if statement is a conditional statement. It allows you to run a block of code conditionally—that is, only if a certain condition is true.

The Anatomy of an if Statement

The basic syntax is simple and builds on concepts we’ve already learned:

if condition:
    # This block of code runs
    # only if the condition is True.

Let’s break down its parts:

  • if: The keyword that starts the statement.
  • condition: This must be a Boolean expression. It can be any code that evaluates to either True or False.
  • :: The colon that signifies the beginning of an indented block of code, which we first discussed in Post #6.
  • The indented block: One or more lines of code, indented with four spaces. This block will be executed only if the condition is True.

if Statements in Action

Let’s look at a few examples to see how this works.

Example 1: When the Condition is True

Here, we’ll check if a person is old enough to vote.

age = 20

# The condition 'age >= 18' evaluates to True
if age >= 18:
    print("You are old enough to vote.")

print("This line runs no matter what.")

When this code runs, Python checks if age >= 18. Since 20 is greater than 18, the expression is True. Because the condition is True, the indented print() statement is executed. The final print statement runs afterward, as it is not indented and thus not part of the if block.

Example 2: When the Condition is False

Now let’s see what happens when the condition is not met.

temperature = 15 # in Celsius

# The condition 'temperature > 25' evaluates to False
if temperature > 25:
    # This block will be skipped
    print("It's a hot day! Wear shorts.")

print("Have a nice day!")

In this case, 15 is not greater than 25, so the expression temperature > 25 evaluates to False. Because the condition is False, Python completely skips the indented block of code and continues execution on the first line after the block.

Example 3: Using Truthy and Falsy Values

We can also use the Truthy/Falsy concept from Post #24 to write very clean and “Pythonic” checks.

user_name = input("Please enter your name: ")

# The condition is the string itself.
# It's Truthy if the user typed something, Falsy if it's empty.
if user_name:
    print(f"Hello, {user_name}!")

This is a very common pattern. The if statement directly evaluates the user_name variable. If the user entered any text, it’s a non-empty string (Truthy), and the greeting is printed. If they just pressed Enter, it’s an empty string (Falsy), and the block is skipped.

What’s Next?

You can now control the flow of your programs! The if statement gives you the power to create a path that is only taken when a condition is met. This is the first and most important step in writing intelligent, responsive code.

Our current if statement is great for doing something when a condition is true, but what if we want to do something else when it’s false? Right now, the program just skips the block and moves on. In Post #26, we will learn how to provide an alternative path of execution by adding an else block to our if statements.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment