Coding/Learn/python/if statements
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 Statements & Conditional Execution
15 / 32 0%
Lesson 15 of 32
6 mins read

If Statements & Conditional Execution

PreviousNext Lesson
In Plain English

An if statement is a fork in the road. If the sign says "Free Pizza", you take the turn. If not, you keep driving straight ahead. Python uses clean 4-space indentation to know what code belongs inside the decision.

The Mental Model

"Think of an umbrella check before leaving home. IF it is raining, you grab your umbrella. If it is sunny, you simply step outside without doing anything special."

Deep Dive: How It Works

The if statement tests an expression for truthiness. If the expression evaluates to True, Python executes the indented block of code beneath it.

If the condition evaluates to False, Python skips the indented block entirely and continues with the first unindented line below.

An if statement always ends with a colon : on the header line. Forgetting the colon is the #1 syntax error beginners encounter.

Syntax Blueprint

if condition:
    # Indented 4 spaces
    run_this_code()

Header line ends with a colon. The code to execute on True must be indented.

PointCore Rules to Remember

Point star
The if Keyword & Colon: Write if condition: - always end the line with a colon.
Point star
Indentation is Law: Lines indented 4 spaces only run if the condition evaluates to True.
Point star
Skip on False: When condition is False, Python jumps straight past the indented lines.

Common Beginner Traps & How to Fix Them

Mistake:Forgetting the colon at the end of the if line: if score > 50

Why it happens: In everyday writing we do not use colons for conditions, causing a SyntaxError.

How to fix: Always add the colon: if score > 50:.

Mistake:Using = instead of ==: if score = 100:

Why it happens: Single = is assignment, not comparison.

How to fix: Use double equals: if score == 100:.

Pro Tip: You can nest if statements inside other if statements, though keeping code flat with logical operators (and/or) is usually cleaner.

Live Interactive Example

Hit Run Code to see it live
Testing Conditions with if
Python 3.12
1
2
3
4
5
6
7
Output Console
Click "Run Code" to view the rendered output.
How it works: Since coffee_cups == 0 evaluated to True, both indented lines executed before normal execution resumed.

Your Turn: Micro Challenge

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

Micro Exercise

Write an if statement check

  • PointGiven temperature = 35, write an if statement to check if temperature > 30.
  • PointInside the block, print "It is hot outside!"
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