Bird
Raised Fist0
Pythonprogramming~10 mins

Python Block Structure and Indentation - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

What will Python raise if the print line is not indented inside the function?

Python
def greet():
print("Hello")
# Python raises: [1]
Drag options to blanks, or click blank then click option'
AIndentationError
BSyntaxError
CNameError
DTypeError
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Confusing IndentationError with SyntaxError β€” IndentationError is a subclass of SyntaxError but more specific.
Thinking missing indentation causes a runtime error instead of a parse-time error.
2fill in blank
medium

How many spaces does Python's PEP 8 standard recommend per indentation level?

Python
# PEP 8 recommends [1] spaces per indent level
def example():
    if True:
        pass  # this line is 2 levels deep
Drag options to blanks, or click blank then click option'
A2
B4
C8
D1
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 2 spaces β€” common in other languages but not Python's standard.
Mixing tabs and spaces which causes IndentationError.
3fill in blank
hard

What does this code print? Pay close attention to which print belongs inside the loop.

Python
for i in range(3):
    print("A")
print("B")
# Total lines printed: [1]
Drag options to blanks, or click blank then click option'
A3
B1
C6
D4
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Thinking both prints are inside the loop β€” only the indented one is.
Forgetting that the unindented print runs once after the loop.
4fill in blank
hard

Fill both blanks. The return statement's indentation decides when the function returns.

Python
def add_until(n):
    total = 0
    for i in range(1, n+1):
        total += i
    return total

print(add_until(3))
# Output: [1]
# return belongs to the [2] block
Drag options to blanks, or click blank then click option'
A6
B1
Cfunction
Dloop
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Thinking return is inside the loop β€” check its indentation level.
Confusing function-level and loop-level indentation.
5fill in blank
hard

Fill all three blanks. Determine each output line based on which block each print belongs to.

Python
x = 10
if x > 5:
    print("big")
    if x > 8:
        print("very big")
print("done")
# Line 1: [1]
# Line 2: [2]
# Line 3: [3]
Drag options to blanks, or click blank then click option'
Abig
Bvery big
Cdone
Dnothing
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Thinking 'done' only prints when conditions are true β€” it is outside all if blocks.
Missing the nested if block β€” the second indent level creates a block inside a block.

Practice

(1/5)
1. What does indentation in Python indicate?
easy
A. Which lines of code belong together in a block
B. The data type of a variable
C. The speed of code execution
D. The name of a function

Solution

  1. 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.
  2. Step 2: Identify what indentation shows

    Indentation shows which lines are connected logically and executed together.
  3. Final Answer:

    Which lines of code belong together in a block -> Option A
  4. Quick 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
A.
def greet():
    print('Hello')
  print('World')
B.
def greet():
print('Hello')
print('World')
C.
def greet():
print('Hello')
    print('World')
D.
def greet():
  print('Hello')
  print('World')

Solution

  1. Step 1: Check indentation inside the function

    All lines inside the function must be indented the same amount to belong to the function block.
  2. 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.
  3. Final Answer:

    def greet():\n print('Hello')\n print('World') -> Option D
  4. Quick 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
A. Start\nFirst loop\nEnd\nStart\nEnd
B. Start\nEnd\nStart\nFirst loop\nEnd
C. Start\nFirst loop\nEnd\nStart\nFirst loop\nEnd
D. Start\nEnd\nStart\nEnd

Solution

  1. Step 1: Trace the loop iterations

    The loop runs twice: i=0 and i=1.
  2. 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.
  3. Final Answer:

    Start\nFirst loop\nEnd\nStart\nEnd -> Option A
  4. Quick 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
A. SyntaxError because of missing colon
B. IndentationError due to inconsistent indentation
C. No error, code runs fine
D. NameError due to undefined variable

Solution

  1. Step 1: Check indentation after if statement

    Lines inside the if block must be indented equally.
  2. Step 2: Identify inconsistent indentation

    The first print is not indented, the second print is indented. This causes an IndentationError.
  3. Final Answer:

    IndentationError due to inconsistent indentation -> Option B
  4. Quick 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
A.
def print_numbers():
    for i in range(1, 4):
        print(i)
    print('Done')
B.
def print_numbers():
    for i in range(1, 4):
    print(i)
    print('Done')
C.
def print_numbers():
    for i in range(1, 4):
        print(i)
        print('Done')
D.
def print_numbers():
for i in range(1, 4):
    print(i)
    print('Done')

Solution

  1. Step 1: Understand desired output

    Each number 1 to 3 should print, then 'Done' on a new line after each number.
  2. 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.
  3. 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.
  4. Final Answer:

    def print_numbers():\n for i in range(1, 4):\n print(i)\n print('Done') -> Option C
  5. Quick 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