Skip to content

Module 5 · Conditional Statements — topic 3 of 10

if-else

if-else chooses between exactly two actions. Learn its syntax, how to choose which case goes first, and when an if-else can be replaced by returning a Boolean directly.

Beginner 10 min read·Lesson 62 of 306 in Python Programming

Learning objectives

  • Write if-else statements that choose between two actions
  • Order the branches for readability
  • Avoid redundant if-else around Booleans

Definition

if-else chooses between exactly two actions. Learn its syntax, how to choose which case goes first, and when an if-else can be replaced by returning a Boolean directly.

Introduction

if-else describes an either/or choice. If the condition is true the first block runs; otherwise the else block runs. Exactly one of the two always runs, never both and never neither.

Why it matters

Many decisions have exactly two outcomes: pass or fail, valid or invalid, in stock or out of stock. Writing them as if-else makes it obvious to a reader that every situation is covered.

Detailed explanation

else has no condition of its own; it catches every case where the if condition is false. It is followed by a colon and its own indented block, aligned with the if.

Because the branches are mutually exclusive and together exhaustive, if-else guarantees that something happens. This is useful for producing a value that must always be set, such as a status message.

Order the branches so the code reads naturally. A common style is to put the normal or expected case first and the exceptional case in else, or to put the shorter branch first so the reader sees both quickly.

If both branches only produce a value, the conditional expression on one line (covered later in this module) may be clearer.

When the branches merely return True or False, the comparison itself is already a Boolean: return age >= 18 replaces a four-line if-else.

Syntax

Python
if condition:
runs_when_true
else:
runs_when_false
  • else lines up with if.
  • else takes no condition.
  • Exactly one of the two blocks runs.

Code examples

Pass or fail

Python
marks = 47
if marks >= 40:
print("Pass")
else:
print("Fail")
Output
Pass

Even or odd

Python
n = 13
if n % 2 == 0:
print(n, "is even")
else:
print(n, "is odd")
Output
13 is odd

More examples

Stock check and a simplified Boolean

Python
stock = {"pen": 12, "ink": 0}
def can_buy(item, qty):
return stock.get(item, 0) >= qty
for item, qty in [("pen", 5), ("ink", 1), ("paper", 2)]:
if can_buy(item, qty):
stock[item] -= qty
print(f"Sold {qty} {item}, {stock[item]} left")
else:
print(f"Cannot sell {qty} {item}")
Output
Sold 5 pen, 7 left
Cannot sell 1 ink
Cannot sell 2 paper

Step-by-step explanation

  • can_buy returns the comparison directly instead of an if-else returning True or False.
  • stock.get(item, 0) treats unknown items such as paper as having zero stock.
  • The if branch reduces stock and reports the sale; the else branch reports a refusal.
  • Each request goes through exactly one of the two branches.

Expected output

Running “Stock check and a simplified Boolean” prints exactly:

TextOutput
Sold 5 pen, 7 left
Cannot sell 1 ink
Cannot sell 2 paper

Practical applications

  • Login pages show the dashboard if credentials are valid, otherwise an error.
  • Payment systems either approve or decline a transaction.
  • Smart lights turn on if motion is detected, otherwise stay off.

Best practices

  • Use if-else when there are exactly two outcomes.
  • Keep both branches short; move long logic into functions.
  • Return comparisons directly instead of wrapping them in if-else.

Common mistakes

Giving else a condition

Avoid

else marks < 40:

Prefer

else:

else never takes a condition. Use elif if you need another test.

Verbose Boolean returns

Avoid

if age >= 18:
return True
else:
return False

Prefer

return age >= 18

The comparison already produces True or False.

Key takeaways

  • if-else runs exactly one of two blocks.
  • else has no condition and catches everything else.
  • A Boolean expression can often replace an if-else that returns True/False.

Try it yourself

  1. 1
    Ask for a number and print whether it is positive or not positive using if-else.
  2. 2
    Rewrite this as one line: if x > 0: result = True else: result = False.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic