FREE PYTHON LESSON · DECISION MAKING

Python if, elif & else for beginners: learn to make decisions

Why does a program choose one action instead of another? Learn Python conditions from zero, see exactly what each branch prints, and finish by building a tiny study-plan adviser in standard Python 3.

By Lumio AI · Published November 1, 2026 · Free, runnable Python 3 examples

What is an if statement in Python?

An if statement runs its indented code only when a condition is true. The optional else runs when that condition is false. You do not need a paid app, model or third-party package to try these examples—just a Python 3 interpreter.

Type the following into a file called conditions.py. Before running it, guess which line appears:

minutes = 25

if minutes >= 20:
    print("Practice goal reached!")
else:
    print("Keep practicing.")

Expected output:

Practice goal reached!

Why? 25 >= 20 is True, so Python runs the first indented block and skips the else. Change minutes to 10; it prints Keep practicing. instead. The colon (:) starts a block; indentation marks the statements inside it.

How does elif differ from else?

elif means “else if.” It checks another condition only when the preceding if or elif did not match. Python chooses the first matching branch in the chain, not every matching branch.

score = 7

if score >= 9:
    print("Excellent")
elif score >= 7:
    print("On track")
else:
    print("Practice again")

Expected output:

On track

Try the boundary values 9, 7 and 6: they print Excellent, On track and Practice again respectively. Put the more selective condition first: checking score >= 7 before score >= 9 would make the excellent branch unreachable for valid scores above 9.

Which comparison and logical operators should you know?

Conditions evaluate to True or False. Use == for equality (not =, which assigns a value); != for not equal; >, <, >= and <= for ordering. Use and when both requirements must hold, or when either may hold, and not to reverse a Boolean.

minutes = 30
has_notes = True

if minutes >= 20 and has_notes:
    print("Ready to review")
else:
    print("Finish your session")

Expected output:

Ready to review

With has_notes = False, the output becomes Finish your session. Python short-circuits Boolean operations when the result is already determined; only rely on that behavior once you understand which expressions actually run.

How do you use if/else with input()?

input() returns text, even if the user types digits. Convert text deliberately and handle invalid input rather than crashing on an unexpected answer:

raw = input("Minutes practiced: ")

try:
    minutes = int(raw)
except ValueError:
    print("Enter a whole number.")
else:
    if minutes >= 20:
        print("Goal reached!")
    elif minutes >= 0:
        print("Try a 20-minute session.")
    else:
        print("Minutes cannot be negative.")

If you type 25, the output is Goal reached!. If you type ten, the output is Enter a whole number.. If you type -1, the last branch rejects it. The else after except is part of try, not the inner if; it runs if the conversion raised no exception.

Mini-project: a rule-based study-plan adviser

This small project combines multiple conditions and uses a function so you can test the outcome without typing user input each time. It suggests a next step using minutes practiced and a quiz score out of 10. This is ordinary rule-based Python, not a machine-learning model or a diagnosis of anyone's ability.

def next_step(minutes, score):
    if minutes < 0 or score < 0 or score > 10:
        return "Check your numbers."
    if minutes < 20:
        return "Practice Python for 20 minutes."
    elif score < 7:
        return "Review mistakes and retry."
    else:
        return "Build a small Python project."

cases = [(0, 9), (25, 3), (25, 7), (-1, 8), (30, 11)]

for minutes, score in cases:
    print(next_step(minutes, score))

Expected output for the five cases, in order:

Practice Python for 20 minutes.
Review mistakes and retry.
Build a small Python project.
Check your numbers.
Check your numbers.

Notice the boundary: score == 7 reaches the project branch only when minutes >= 20. A negative minute count and a score above 10 are both rejected first. For a simple first exercise, we deliberately assume integer inputs; a real input form must also validate types and handle missing values. The examples use no network requests and no AI API.

Challenge: change the practice threshold to 30 minutes. Which case outputs change? Write down your prediction, modify the function, and run it again. This is a small but meaningful form of testing.

Four common Python if/else mistakes

  1. Using = instead of ==: if score = 7: is invalid syntax; use if score == 7:.
  2. Forgetting the colon or indentation: write if ready: and indent its body consistently. Python uses indentation to group statements.
  3. Ordering broad conditions first: an early if score >= 0 will catch every valid non-negative score, hiding later branches.
  4. Comparing text directly with numbers: input() returns a string; calling int() without handling bad input may cause a ValueError.

Read the traceback line and verify one condition at a time. When debugging, print a value such as print(score, score >= 7) to see both the data and the Boolean result.

Five Python condition exercises with worked solutions

Predict each answer before revealing the solutions. Then run the code yourself, including one boundary value you choose.

  1. Given temperature = 18, print Warm if it is at least 25, otherwise print Cool. What prints?
  2. Given score = 7, print Passed for scores at least 7, otherwise Retry. What happens at the boundary?
  3. Given minutes = 0 and has_notes = True, print Review only when minutes are at least 20 and notes exist. Otherwise print Start practice.
  4. Given name = "Ada", print the name only if it is non-empty; otherwise print Enter a name.
  5. Given level = 2, print Advanced if level is at least 3, Intermediate if at least 2, otherwise Beginner.
Reveal the worked solutions and outputs
# 1: Cool
temperature = 18
if temperature >= 25:
    print("Warm")
else:
    print("Cool")

# 2: Passed
score = 7
print("Passed" if score >= 7 else "Retry")

# 3: Start practice
minutes, has_notes = 0, True
if minutes >= 20 and has_notes:
    print("Review")
else:
    print("Start practice")

# 4: Ada
name = "Ada"
if name:
    print(name)
else:
    print("Enter a name")

# 5: Intermediate
level = 2
if level >= 3:
    print("Advanced")
elif level >= 2:
    print("Intermediate")
else:
    print("Beginner")

The second solution uses Python's conditional expression, a short way to choose between two values; ordinary if/else is often clearer while you're learning. A string such as "" is false in a condition, while "Ada" is true.

What should you learn after if, elif and else?

Use conditions with a Python for/while loops lesson to repeat decisions, then move to functions and return values to reuse your code. Try 10 additional Python exercises to test combinations of conditions, loops and collections. If you need the first step, go to our Python tutorial from zero.

From there, the free seven-session Python for AI plan adds a small tested retriever, and the Python FAQ chatbot tutorial shows how rule-based decisions form useful software without pretending to be generative AI.

For the language rules themselves, see the official Python if-statement documentation and the official input() reference. Continue learning with beginner-to-AI engineering roadmap.