Python If Statements: Making Decisions in Code

This post introduces Python if statements for beginners, covering the basics of conditional logic with if, elif, and else. It explains comparison operators, combining conditions with and/or/not, and includes practical code examples. Readers finish with a clear understanding of how to make decisions

Python If Statements: Making Decisions in Code

One of the most fundamental skills in programming is teaching your code to make decisions. In Python, this is done with if statements. They allow your program to evaluate a condition and choose a path based on whether that condition is true or false. Think of it like a fork in the road: your code looks at the sign, makes a choice, and heads in one direction.

The Basic If Statement

The simplest form of an if statement checks a single condition. If that condition evaluates to True, the indented block of code underneath it runs. If it evaluates to False, Python skips that block entirely.

temperature = 35

if temperature > 30:
    print("It's a hot day!")

Notice the colon at the end of the if line, and the indented line below it. Indentation is not optional in Python; it is how the language knows which code belongs inside the if block. The standard convention is four spaces.

Adding Else and Elif

A lone if is useful, but most decisions have more than one outcome. Python gives you two additional keywords to handle this: else and elif (short for "else if").

Use else to define what happens when the original condition is false:

temperature = 18

if temperature > 30:
    print("It's a hot day!")
else:
    print("Enjoy the mild weather.")

Use elif when you have multiple conditions to check in sequence:

temperature = 18

if temperature > 30:
    print("It's a hot day!")
elif temperature > 20:
    print("Nice and warm outside.")
elif temperature > 10:
    print("A bit cool, grab a jacket.")
else:
    print("It's cold out there!")

Python works through the conditions from top to bottom and stops at the first one that is true. Once a match is found, the rest of the elif and else blocks are skipped.

Comparison Operators

Your if statements rely on comparison operators to build conditions. Here are the ones you will use most often:

  • ==: equal to
  • !=: not equal to
  • >: greater than
  • <: less than
  • >=: greater than or equal to
  • <=: less than or equal to

A common beginner mistake is using a single equals sign (=) inside an if statement. A single = is for assignment; a double == is for comparison.

Combining Conditions with And, Or, and Not

Sometimes one condition is not enough. Python lets you combine multiple conditions using and, or, and not:

username = "admin"
logged_in = True

if username == "admin" and logged_in:
    print("Welcome to the admin panel.")

age = 17

if not age >= 18:
    print("Access restricted to adults only.")

With and, both conditions must be true for the block to run. With or, only one needs to be true. With not, you invert the result of a condition.

A Practical Example

Here is a short script that puts everything together. Imagine a simple login check for a network tool:

username = input("Enter your username: ")
password = input("Enter your password: ")

if username == "netadmin" and password == "cisco123":
    print("Login successful. Welcome!")
elif username == "netadmin":
    print("Incorrect password. Try again.")
else:
    print("Username not recognised.")

This is a realistic pattern you will see in scripts, web apps, and automation tools. The logic is straightforward but powerful: check a condition, respond accordingly.

What's Next

Now that your code can make decisions, the next natural step is making it repeat actions. The next post in this series covers Python loops, specifically for and while loops, which let you iterate through data and automate repetitive tasks. If you have been thinking about writing scripts to process lists of devices or IP addresses, loops are exactly the tool you need.