Coding/Learn/python/if else
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnIf-Else Branching (Dual Paths)
16 / 32 0%
Lesson 16 of 32
6 mins read

If-Else Branching (Dual Paths)

PreviousNext Lesson
In Plain English

Life is rarely one-sided. When a user logs in, either their password is correct (grant access), OR it is wrong (show error). The else block gives your code a guaranteed fallback path when the if condition evaluates to False.

The Mental Model

"Imagine a coin toss. Either it lands on Heads (you win), OR it lands on Tails (you lose). There is no third option where nothing happens. If-else guarantees exactly one path is chosen."

Deep Dive: How It Works

The else statement never takes a condition of its own: it simply catches everything that did not pass the if check.

Like the if statement, the else line must end with a colon : and its code block must be indented by 4 spaces.

Guarantee: In an if-else structure, exactly one of the two blocks will execute. They can never both run, and neither can be skipped.

Syntax Blueprint

if condition:
    # Runs if condition is True
    do_option_a()
else:
    # Runs if condition is False
    do_option_b()

Either block A or block B runs, guaranteed. else takes no condition.

PointCore Rules to Remember

Point star
The else Fallback: else catches all cases where the if condition failed.
Point star
No Condition on else: Writing else condition: is illegal syntax. else stands alone with a colon.
Point star
Mutually Exclusive: Exactly one branch executes on every single run.

Common Beginner Traps & How to Fix Them

Mistake:Putting a condition on else: else age < 18:

Why it happens: Thinking else needs a check. In Python, only elif takes a condition.

How to fix: Just write else: or use elif age < 18: if you need a specific check.

Pro Tip: Python also supports one-line ternary expressions: status = "Pass" if score >= 50 else "Fail".

Live Interactive Example

Hit Run Code to see it live
Authentication & Access Control
Python 3.12
1
2
3
4
5
6
7
8
Output Console
Click "Run Code" to view the rendered output.
How it works: Since 15 > 20 is False, Python skipped the if block and executed the else branch.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create an pass/fail check

  • PointGiven score = 45, write an if-else statement.
  • PointIf score >= 50, print "Passed".
  • PointOtherwise (else), print "Try Again".
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates