Python If Statements & Conditional Execution
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.
"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.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Forgetting the colon at the end of the if line: if score > 50Why 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:.
Using = instead of ==: if score = 100:Why it happens: Single = is assignment, not comparison.
How to fix: Use double equals: if score == 100:.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Write an if statement check
Given temperature = 35, write an if statement to check if temperature > 30.
Inside the block, print "It is hot outside!"
Finished reading and practicing?
Mark this lesson as completed to update your course progress.