Python uses indentation (spaces or tabs) to group code together. This helps the computer know which lines belong together in a block.
Python Block Structure and Indentation
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
if condition: # indented block of code statement1 statement2 # code here is outside the block
Indentation must be consistent: use the same number of spaces or tabs for each block.
Python does not use curly braces { } like some other languages; indentation shows blocks.
Examples
Python
if x > 0: print("Positive") print("Number")
Python
for i in range(3): print(i) print("Done")
Python
def greet(): print("Hello") print("Welcome") greet()
Sample Program
This program uses indentation to group code inside the function and inside the if-elif-else blocks. It prints messages based on the number.
Python
def check_number(num): if num > 0: print(f"{num} is positive") elif num == 0: print("Number is zero") else: print(f"{num} is negative") check_number(5) check_number(0) check_number(-3)
Important Notes
Always use either spaces or tabs for indentation, never mix both in the same file.
Most editors can help by automatically indenting code for you.
Indentation errors cause Python to stop running and show an error message.
Summary
Python uses indentation to show which lines of code belong together.
Indentation must be consistent and is required for blocks like functions, loops, and conditionals.
Proper indentation makes your code easier to read and run correctly.
Practice
1. What does indentation in Python indicate?
easy
Solution
Step 1: Understand Python's use of indentation
Python uses indentation to group lines of code that belong to the same block, like inside a function or loop.Step 2: Identify what indentation shows
Indentation shows which lines are connected logically and executed together.Final Answer:
Which lines of code belong together in a block -> Option AQuick Check:
Indentation = Code blocks [OK]
Hint: Indentation groups code lines into blocks [OK]
Common Mistakes:
- Thinking indentation shows variable types
- Confusing indentation with execution speed
- Assuming indentation names functions
2. Which of the following code snippets has correct indentation in Python?
easy
Solution
Step 1: Check indentation inside the function
All lines inside the function must be indented the same amount to belong to the function block.Step 2: Identify the snippet with consistent indentation
def greet(): print('Hello') print('World')has both print statements indented equally under the function, which is correct.Final Answer:
def greet():\n print('Hello')\n print('World') -> Option DQuick Check:
Consistent indentation inside function = Correct [OK]
Hint: Indent all block lines equally under the header [OK]
Common Mistakes:
- Mixing indentation levels inside a block
- Not indenting any lines inside a function
- Indenting only some lines inconsistently
3. What will be the output of this code?
for i in range(2):
print('Start')
if i == 0:
print('First loop')
print('End')medium
Solution
Step 1: Trace the loop iterations
The loop runs twice: i=0 and i=1.Step 2: Check prints for each iteration
For i=0: prints 'Start', then 'First loop' (because i==0), then 'End'. For i=1: prints 'Start' and 'End' only.Final Answer:
Start\nFirst loop\nEnd\nStart\nEnd -> Option AQuick Check:
Loop with condition prints extra line only once [OK]
Hint: Follow indentation to see which prints run inside conditions [OK]
Common Mistakes:
- Ignoring indentation and printing 'First loop' twice
- Mixing order of printed lines
- Assuming all prints run every iteration
4. Identify the error in this code:
if True:
print('Yes')
print('Still Yes')medium
Solution
Step 1: Check indentation after if statement
Lines inside the if block must be indented equally.Step 2: Identify inconsistent indentation
The first print is not indented, the second print is indented. This causes an IndentationError.Final Answer:
IndentationError due to inconsistent indentation -> Option BQuick Check:
Inconsistent indentation = IndentationError [OK]
Hint: All lines in a block must have same indentation [OK]
Common Mistakes:
- Thinking missing colon causes error here
- Assuming code runs without indentation
- Confusing IndentationError with NameError
5. You want to write a function that prints numbers 1 to 3, each followed by 'Done' on a new line. Which code correctly uses indentation to achieve this?
def print_numbers():
for i in range(1, 4):
print(i)
print('Done')hard
Solution
Step 1: Understand desired output
Each number 1 to 3 should print, then 'Done' on a new line after each number.Step 2: Check indentation for print statements
To print 'Done' after each number, both print(i) and print('Done') must be inside the for loop, indented equally.Step 3: Identify correct option
def print_numbers(): for i in range(1, 4): print(i) print('Done')has both prints indented inside the loop, so 'Done' prints after each number.Final Answer:
def print_numbers():\n for i in range(1, 4):\n print(i)\n print('Done') -> Option CQuick Check:
Same indentation inside loop = prints per iteration [OK]
Hint: Indent all loop actions equally to repeat each time [OK]
Common Mistakes:
- Indenting 'Done' outside loop causing single print
- Not indenting loop body at all
- Mixing indentation levels inside loop
