Practice: Python Indentation

« Read the chapter

Q1. Indent an if block

BeginnerCoding

Write a program that sets temperature = 31 and, if it is above 30, prints Hot and then Drink water (both inside the if). Then print End outside the if.

Goal: Write a basic if block.

Q2. Loop with indented body

BeginnerCoding

Use a for loop over range(3) to print the number and the word step, for example 0 step. Print Over after the loop.

Goal: Indent a loop body.

Q3. Even or odd function

IntermediateCoding

Write a function parity(n) that returns even or odd. Print parity(7) and parity(10).

Goal: Nest blocks inside a function.

Q4. Count positive numbers

IntermediateCoding

Given values = [3, -1, 4, -5, 9], count how many are positive using a loop and an if. Print the count.

Goal: Combine loops and conditions with correct nesting.

Q5. Multiplication table row

BeginnerCoding

Print the table of 4 up to 5 times, each line like 4 x 1 = 4.

Goal: Write a loop with an indented statement.

Q6. Placeholder function

BeginnerCoding

Define a function later() with no body yet (use pass), then call it and print Still works.

Goal: Use pass for an empty block.

Q7. Nested loops pattern

AdvancedCoding

Print a triangle of stars with three rows (1, 2, 3 stars) using nested loops or string multiplication in a loop.

Goal: Handle two levels of nesting.

Q8. Which lines run?

BeginnerOutput

What does this print?

Code
x = 5
if x > 3:
    print("A")
    print("B")
print("C")

Goal: Trace an if block.

Q9. Outside the block

BeginnerOutput

What does this print?

Code
x = 1
if x > 3:
    print("A")
print("B")

Goal: Know which line is outside the block.

Q10. Loop and final line

BeginnerOutput

What does this print?

Code
for i in range(2):
    print("hi")
    print(i)
print("bye")

Goal: Trace a loop body.

Q11. Inner if inside a loop

IntermediateOutput

What does this print?

Code
for n in range(1, 5):
    if n % 2 == 0:
        print(n)
    print("-")

Goal: Compare nested and loop-level statements.

Q12. Same line, different indent

IntermediateOutput

What does this print?

Code
total = 0
for i in range(3):
    total += i
    print(total)

Goal: See how indentation changes output.

Q13. Print after the loop

IntermediateOutput

What does this print?

Code
total = 0
for i in range(3):
    total += i
print(total)

Goal: Compare to the previous example.

Q14. Else belongs to which if

AdvancedOutput

What does this print?

Code
for n in [1, 2]:
    if n == 1:
        print("one")
    else:
        print("other")
        print("end", n)

Goal: Read nested else blocks.

Q15. Fix the missing indent

BeginnerDebugging

This code raises IndentationError. Fix it.

Code
name = "Ria"
if name == "Ria":
print("Hello Ria")

Goal: Fix expected indented block.

Q16. Fix the unexpected indent

BeginnerDebugging

This code raises an error. Fix it.

Code
price = 100
    tax = 18
print(price + tax)

Goal: Fix unexpected indent.

Q17. Fix the mixed indentation

IntermediateDebugging

The lines in this loop body use different indentation. Fix the code so it prints 1 then 2.

Code
for i in [1, 2]:
    print(i)
  print("x")

Goal: Keep block indentation consistent.

Q18. Fix the loop logic

IntermediateDebugging

The program should print the total only once at the end, but prints it every turn. Fix it.

Code
total = 0
for n in [1, 2, 3]:
    total += n
    print(total)

Goal: Understand how indent changes meaning.

Q19. Why indentation?

BeginnerConceptual

Explain in two or three sentences why Python uses indentation instead of curly brackets.

Goal: Explain the purpose of indentation.

Q20. Tabs versus spaces

IntermediateConceptual

What is the recommended indentation in PEP 8 and what problem can mixing tabs and spaces cause?

Goal: Know the style rule.