DEVOLOGIST</>

Python if / else Statement: Make Decisions in Your Code

Python
28/07/20266 min read

Introduction

Until now, many beginner Python programs simply run from top to bottom. But real programs need to make decisions based on different situations.

For example:

This is where if / else comes in. If you want to explore more beginner-friendly tutorials, visit our Python section. You can also review Python Math: A Complete Guide to Arithmetic Operators if you want to strengthen the math skills used in conditions.

What is if / else in Python?

The if statement lets your program check a condition.

If the condition is True, Python runs one block of code.

If the condition is False, Python runs the else block instead.

Basic Syntax

age = 20
if age >= 18:
print("You are an adult")
else:
print("You are under 18")

How This Works

Let’s break it down:

Why Indentation Matters

Python uses indentation to know which code belongs inside the if or else block.

age = 20
if age >= 18:
print("Access granted")
print("Welcome!")
else:
print("Access denied")

Both lines under if run only if the condition is true.

Real Example with User Input

A common use case is making decisions based on what the user types.

age = int(input("Enter your age: "))
if age >= 18:
print("You can sign up")
else:
print("Sorry, you must be at least 18")

This program asks the user for their age and makes a decision based on the answer.

Comparison Operators You Will Use with if

Here are the most common operators used in conditions:

OperatorMeaning
==equal to
!=not equal to
>greater than
<less than
>=greater than or equal to
<=less than or equal to

Example:

score = 75
if score >= 50:
print("You passed")
else:
print("You failed")

Mini Project: Even or Odd Checker

This is a great beginner project because it combines input, math, and conditions.

number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
else:
print("Odd number")

Common Beginner Mistakes

1. Forgetting the colon :

Wrong:

if age >= 18
print("Adult")

Correct:

if age >= 18:
print("Adult")

2. Wrong indentation

Wrong:

if age >= 18:
print("Adult")

Correct:

if age >= 18:
print("Adult")

3. Using = instead of ==

Wrong:

if age = 18:

Correct:

if age == 18:

FAQ

What is if / else in Python?

The if / else statement lets your program make decisions. If a condition is true, Python runs one block of code. If it is false, Python runs the else block instead.

Why is indentation important in Python if statements?

Python uses indentation to determine which lines belong inside the if or else block. Incorrect indentation can cause errors or unexpected results.

What is the difference between = and == in Python?

A single equals sign = assigns a value to a variable, while == compares two values inside a condition.

Conclusion

The if / else statement is one of the most important tools in Python. It allows your programs to make decisions and respond differently depending on the situation.

Once you understand if / else, you can start building:

In the next lesson, you can continue with comparison operators or logical operators to make your conditions even more powerful. For more beginner tutorials, browse the Python category.