Python else Statement

Ka Kavitha V Updated 03 Oct 2026
4 min read ·Lesson 21 of 23

else Statement

An if statement decides what happens when a condition is true. But programs usually also need to say what happens otherwise. The else keyword defines that default block: it runs when none of the preceding conditions are true.

What Is the else Keyword?

The else keyword defines a block of code that runs when all preceding if and elif conditions evaluate to False.

Syntax

if condition:
    # runs when the condition is True
else:
    # runs when the condition is False

With elif:

if condition1:
    # runs when condition1 is True
elif condition2:
    # runs when condition1 is False and condition2 is True
else:
    # runs when every condition above is False

Notice that else has no condition of its own. It is followed directly by a colon.

Basic Usage of else

Example: Comparing Two Values

x = 120
y = 85

if y > x:
    print("y is larger than x")
elif x == y:
    print("x and y have the same value")
else:
    print("x is larger than y")

Expected output:

x is larger than y

Explanation:

  1. The if condition 85 > 120 is False.
  2. The elif condition 120 == 85 is also False.
  3. Because no condition matched, Python runs the else block as the final option.

These three branches cover every possible relationship between two numbers — greater, equal, or smaller — so exactly one message is always printed.

Using else Without elif

An else can directly follow an if when you only need a two-way, yes-or-no decision.

Example: Simple Condition Check

age = 17

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

Expected output:

Access denied

Explanation:

  • If the condition is True, the first block runs.
  • Otherwise, the else block runs.
  • Exactly one of the two blocks always runs — never both, never neither.

How the else Statement Works

  • Python evaluates conditions from top to bottom.
  • If no condition matches, the else block runs.
  • else must always be the last part of the structure.
  • No elif can appear after else.
  • There can be at most one else per if statement.

Placing elif after else is a syntax error:

score = 70

if score > 80:
    print("High")
else:
    print("Low")
elif score > 60:
    print("Medium")

Output:

SyntaxError: invalid syntax

Example: Even or Odd Number Check

value = 42

if value % 2 != 0:
    print("Odd number")
else:
    print("Even number")

Expected output:

Even number

Why this works:

  • The if condition checks for odd numbers: a remainder that is not 0 when dividing by 2.
  • 42 % 2 is 0, so the condition is False.
  • Every integer is either odd or even, so else automatically handles all the even cases.

The Complete if–elif–else Structure

Combining if, elif, and else lets you handle several mutually exclusive outcomes in a clean and readable way.

Example: Weather Status Program

temp = 18

if temp >= 35:
    print("Very hot weather")
elif temp >= 25:
    print("Warm weather")
elif temp >= 15:
    print("Mild weather")
else:
    print("Cold weather")

Expected output:

Mild weather
TemperatureBranch That Runs
35 or aboveVery hot weather
25–34Warm weather
15–24Mild weather
Below 15Cold weather (else)

Because the conditions are checked from the highest threshold down, each temperature lands in exactly one range, and else catches everything below 15.

else as a Fallback Mechanism

The else block is commonly used as a safety net for:

  • Input validation — rejecting input that does not meet the rules
  • Error handling — responding to unexpected situations
  • Default responses — providing a sensible result when nothing else applies

Example: User Input Validation

email = ""

if email.strip():
    print("Email accepted")
else:
    print("Invalid input: Email is required")

Expected output:

Invalid input: Email is required

How it works:

  • email.strip() removes spaces from both ends of the string.
  • An empty string is falsy, so the if condition is False.
  • The else block reports the problem.

Using strip() also rejects input that contains only spaces, such as " ", which a plain if email: check would accept.

Example: Default Discount

membership = "silver"

if membership == "gold":
    discount = 20
elif membership == "platinum":
    discount = 30
else:
    discount = 5

print(f"Discount: {discount}%")

Expected output:

Discount: 5%

The else branch guarantees that discount always has a value, so the final print() never fails with a NameError.

Practical Example: Login Check

saved_password = "py@2026"
entered_password = "py@2025"

if entered_password == saved_password:
    print("Login successful")
else:
    print("Incorrect password. Please try again.")

Expected output:

Incorrect password. Please try again.

Common Mistakes

MistakeExampleProblem
Adding a condition to elseelse x > 5:SyntaxError; use elif for conditions
Forgetting the colonelseSyntaxError: expected ':'
elif after elseelse: … elif ...:SyntaxError
Misaligned elseelse indented differently from its ifSyntaxError or the else attaches to the wrong if
Using else when one case is impossible to reachOverlapping conditionsReview the order and ranges of conditions
  • Python if Statement — conditional execution basics
  • Python elif Statement — checking several conditions in order
  • Python Shorthand if — writing if–else on a single line
  • Python Nested If — decisions inside decisions

0 Comments

Reviewed before they appear

No comments yet.

Python
Ask about this post
AI Ask about this post

Ask questions about Python else Statement and get answers drawn from it.

Signed-in readers only.