Python Block Structure and Indentation - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we write Python code, the way we organize it with indentation affects how the computer reads it.
We want to see how the structure of blocks influences how many steps the program takes.
Analyze the time complexity of the following code snippet.
def print_numbers(n):
for i in range(n):
if i % 2 == 0:
print(i)
else:
print(-i)
This code prints numbers from 0 to n-1, printing positive even numbers and negative odd numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from 0 to n-1.
- How many times: Exactly n times, once for each number.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times |
| 100 | About 100 times |
| 1000 | About 1000 times |
Pattern observation: The steps increase directly with n; double n means double steps.
Time Complexity: O(n)
This means the program takes longer in a straight line as the input number grows.
[X] Wrong: "Because there is an if-else inside the loop, the time doubles or grows faster."
[OK] Correct: The if-else only chooses between two simple actions each time; it does not add extra loops or repeated work.
Understanding how Python reads blocks and counts steps helps you explain your code clearly and think about efficiency.
"What if we added another loop inside the if block? How would the time complexity change?"
Practice
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]
- Thinking indentation shows variable types
- Confusing indentation with execution speed
- Assuming indentation names functions
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]
- Mixing indentation levels inside a block
- Not indenting any lines inside a function
- Indenting only some lines inconsistently
for i in range(2):
print('Start')
if i == 0:
print('First loop')
print('End')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]
- Ignoring indentation and printing 'First loop' twice
- Mixing order of printed lines
- Assuming all prints run every iteration
if True:
print('Yes')
print('Still Yes')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]
- Thinking missing colon causes error here
- Assuming code runs without indentation
- Confusing IndentationError with NameError
def print_numbers():
for i in range(1, 4):
print(i)
print('Done')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]
- Indenting 'Done' outside loop causing single print
- Not indenting loop body at all
- Mixing indentation levels inside loop
