Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python if, elif, and else Statements

Control flow · Tutorial 9

Python if, elif, and else Statements

Control program flow with conditions, nested branches, and clear boolean expressions.

Open the compilerPractice exercises
beginner8 min readUpdated November 19, 20263 learning goals
On this page
  1. Adding alternatives
  2. Nested conditions
  3. Conditional expressions
  4. Common mistakes
  5. A complete example
  6. Practice

What you will learn

  • Write if, elif, and else branches
  • Understand indentation as block structure
  • Avoid common condition mistakes
Before this lesson: Python Booleans and Comparison Operators

An if statement runs a block only when its condition is true.

Python
temperature = 31

if temperature > 30:
    print("It is hot outside.")

The indented line belongs to the if. Python uses indentation instead of curly braces, so consistent spacing is part of the syntax. Four spaces is the standard.

Adding alternatives#

Python
score = 87

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"

print(grade)

elif is checked only when every previous condition was false. The else block runs when none matched. Conditions are tested from top to bottom, so order matters.

Nested conditions#

Python
age = 20
has_ticket = False

if age >= 18:
    if has_ticket:
        print("Entry allowed")
    else:
        print("Buy a ticket first")
else:
    print("Entry requires an adult")

Nesting is useful when the inner condition only makes sense after the outer one. Avoid deep nesting; extract a function or combine conditions with and when it stays readable.

Conditional expressions#

For a short either/or value, use a conditional expression:

Python
number = 7
label = "even" if number % 2 == 0 else "odd"
print(label)

This is an expression, not a statement. Use it when each branch is a single simple value.

Common mistakes#

  • Using = instead of == in a comparison.
  • Forgetting the colon at the end of if, elif, or else.
  • Mixing tabs and spaces. Configure your editor to insert spaces.
  • Leaving an empty block. Use pass as a temporary placeholder.
  • Writing if x == 1 or 2:. This is always true; write if x == 1 or x == 2: or if x in (1, 2):.

Keep conditions positive

Prefer if is_valid: over if not is_invalid:. Positive conditions are easier to read, especially when combined with not.

A complete example#

Python
username = "ada"
password = "python123"

if not username:
    print("Username is required")
elif len(password) < 8:
    print("Password must contain at least 8 characters")
elif username == password:
    print("Username and password must differ")
else:
    print("Credentials look valid")

Practice#

Write a program that reads a score from 0 to 100 and prints a grade. Handle invalid scores separately. Then extend it to print a message for a perfect score.

Previous tutorialPython Booleans and Comparison OperatorsNext tutorialPython while Loops

Related tutorials

beginner

Python while Loops

Repeat code until a condition changes, avoid infinite loops, and use counters and sentinels safely.

8 min read
beginner

Python for Loops

Iterate over strings, lists, ranges, and other iterables with Python's for loop.

8 min read

On this page

  1. Adding alternatives
  2. Nested conditions
  3. Conditional expressions
  4. Common mistakes
  5. A complete example
  6. Practice

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.