🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #72

Python Else


The Else Keyword

The else keyword catches anything which isn't caught by the preceding conditions.

The else statement is executed when the if condition (and any elif conditions) evaluate to False.

Example

a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b".


Else Without Elif

You can also have an else without the elif:

Example

a = 200
b = 33
if b > a:
  print("b is greater than a")
else:
  print("b is not greater than a")

This creates a simple two-way choice: if the condition is true, execute one block; otherwise, execute the else block.


How Else Works

The else statement provides a default action when none of the previous conditions are true. Think of it as a "catch-all" for any scenario not covered by your if and elif statements.

Note: The else statement must come last. You cannot have an elif after an else.

Example

number = 7

if number % 2 == 0:
  print("The number is even")
else:
  print("The number is odd")

Complete If-Elif-Else Chain

You can combine if, elif, and else to create a comprehensive decision-making structure.

Example

temperature = 22

if temperature > 30:
  print("It's hot outside!")
elif temperature > 20:
  print("It's warm outside")
elif temperature > 10:
  print("It's cool outside")
else:
  print("It's cold outside!")

Else as Fallback

The else statement acts as a fallback that executes when none of the preceding conditions are true. This makes it useful for error handling, validation, and providing default values.

Example

username = "Emil"

if len(username) > 0:
  print(f"Welcome, {username}!")
else:
  print("Error: Username cannot be empty")

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Python Else – FAQs

Quick answers about learning Python Else in Python.

This free note from CodingNow 2.0 explains Python Else in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Python Else, is 100% free with no signup required.
With focused practice, most students grasp Python Else in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now