The Golden Rule: Indentation
Python has a famous rule: Indentation is not decoration. It is part of the program.
If you come from languages such as Java, C, or C++, you may be used to code like:
if (age >= 18) { System.out.println("Adult");}The curly braces tell the programming language which statements belong to the if block.
Python does things differently. Python uses indentation.
Python Does Not Use Curly Braces for Blocks
In Python, you can write:
if age >= 18: print("Adult")Notice the spaces before print. Those spaces tell Python: "This statement belongs to the if block."
There are no curly braces around it. This is one of Python's most recognizable features.
What Is Whitespace?
Whitespace includes things such as:
- Spaces
- Tabs
- New lines
For indentation, we normally use spaces. A common Python convention is four spaces per indentation level.
For example:
if age >= 18: print("You are an adult")The print statement is indented.
Think of Indentation Like a Folder
Imagine your computer has folders:
School
Mathematics
Science
Computer Science
The indentation visually shows that the three subjects belong inside School. Python uses a similar visual structure.
if condition: statement statement statementThe indented statements belong to the if block.
The if Statement
An if statement allows a program to make a decision.
Imagine a security guard checking someone's age. The rule is: If the person is 18 or older, allow entry.
In Python:
age = 20if age >= 18: print("Entry allowed")Python checks: Is age greater than or equal to 18?
Since 20 >= 18 is True, Python executes: print("Entry allowed")
What Does the Colon Mean?
Notice:
if age >= 18:There is a colon at the end. The colon tells Python: "A new block of code is starting."
The next indented statements form that block. So:
if age >= 18: print("Entry allowed")has two important pieces:
Condition → if age >= 18:Block → print("Entry allowed")
if and else
Programs often need two possible paths.
For example: If the person is 18 or older, allow entry. Otherwise, deny entry.
Python:
age = 16if age >= 18: print("Entry allowed")else: print("Entry denied")Here, Python chooses one of two blocks.
graph TD
A[Start] --> B{Check age >= 18}
B -- Yes --> C[Execute if block]
B -- No --> D[Execute else block]
C --> E[Continue]
D --> E
Why Is Indentation Beautiful?
At first, indentation might seem annoying. But consider this:
if age >= 18: print("Entry allowed")You can immediately see which statement belongs to the if. There is no extra pair of symbols surrounding the block. The visual structure and logical structure are closely connected. This makes Python code easy to read.
Compare the idea:
if condition: do_something()It almost reads like English: "If this condition is true, do something."
Why Is Indentation Dangerous?
The same feature that makes Python beautiful can cause problems.
Consider:
age = 20if age >= 18: print("You are an adult") print("You can enter")Both statements are indented. Therefore, both belong to the if block.
But look at this:
age = 20if age >= 18: print("You are an adult")print("This runs after the if block")The second print is not indented. Therefore, it is outside the if block. The indentation changes the meaning of the program.
A Tiny Space Can Change Logic
Compare these two programs.
Version 1
if logged_in: print("Welcome") print("Show dashboard")Both statements run when the condition is true.
Version 2
if logged_in: print("Welcome")print("Show dashboard")Now "Show dashboard" runs regardless of the condition. That is a major logical difference.
Key Takeaway: If a statement is indented, it runs only when the parent condition is True. If you accidentally dedent it to the left margin, Python treats it as global code that runs unconditionally every single time!
Nested Blocks
Python blocks can exist inside other blocks.
For example:
age = 20has_ticket = Trueif age >= 18: if has_ticket: print("Welcome")Think of this as folders inside folders:
if age >= 18
└── if has_ticket
└── print("Welcome")
Each deeper level gets additional indentation. A simplified visual:
graph TD
A[Program] --> B[if age >= 18]
B --> C[Indented block]
C --> D[if has_ticket]
D --> E[More deeply indented block]
E --> F[print Welcome]
Scope
Indentation is also closely connected to the idea of scope.
Scope answers a simple question: "Where is this name available or meaningful?"
You do not need to master Python scope yet. For now, understand that Python programs have different regions, and names created in certain regions may have limited visibility.
For example, functions create their own local scope:
def greet(): message = "Hello" print(message)The variable message belongs to the function's local scope. You will learn functions and scope in much more detail later.
Spaces or Tabs?
Python code can use tabs or spaces for indentation, but mixing them carelessly can cause errors and confusion.
The standard recommendation is: Use 4 spaces per indentation level.
Most modern Python editors automatically handle this for you.
Common Beginner Mistake
This is wrong:
if age >= 18:print("Adult")Python expects an indented block after the if.
Correct:
if age >= 18: print("Adult")Another problem is inconsistent indentation:
if age >= 18: print("Adult") print("Allowed")The indentation levels do not match. Python may raise an indentation-related error.
[!TIP] Key Takeaways
- Python uses indentation to define code blocks.
- Python does not normally use curly braces for blocks.
- A colon introduces a new block.
- Indented statements belong to that block.
- Four spaces per indentation level is the standard convention.
- Incorrect indentation can cause errors or change program behavior.
- Indentation makes Python code visually clean.
- Indentation is part of Python's syntax, not just formatting.
Golden rule: In Python, whitespace can change what your program means. Respect the indentation.