Practice: Python Indentation
Q1. Indent an if block
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
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
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
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
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
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
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?
What does this print?
x = 5
if x > 3:
print("A")
print("B")
print("C")Goal: Trace an if block.
Q9. Outside the block
What does this print?
x = 1
if x > 3:
print("A")
print("B")Goal: Know which line is outside the block.
Q10. Loop and final line
What does this print?
for i in range(2):
print("hi")
print(i)
print("bye")Goal: Trace a loop body.
Q11. Inner if inside a loop
What does this print?
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
What does this print?
total = 0
for i in range(3):
total += i
print(total)Goal: See how indentation changes output.
Q13. Print after the loop
What does this print?
total = 0
for i in range(3):
total += i
print(total)Goal: Compare to the previous example.
Q14. Else belongs to which if
What does this print?
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
This code raises IndentationError. Fix it.
name = "Ria"
if name == "Ria":
print("Hello Ria")Goal: Fix expected indented block.
Q16. Fix the unexpected indent
This code raises an error. Fix it.
price = 100
tax = 18
print(price + tax)Goal: Fix unexpected indent.
Q17. Fix the mixed indentation
The lines in this loop body use different indentation. Fix the code so it prints 1 then 2.
for i in [1, 2]:
print(i)
print("x")Goal: Keep block indentation consistent.
Q18. Fix the loop logic
The program should print the total only once at the end, but prints it every turn. Fix it.
total = 0
for n in [1, 2, 3]:
total += n
print(total)Goal: Understand how indent changes meaning.
Q19. Why indentation?
Explain in two or three sentences why Python uses indentation instead of curly brackets.
Goal: Explain the purpose of indentation.
Q20. Tabs versus spaces
What is the recommended indentation in PEP 8 and what problem can mixing tabs and spaces cause?
Goal: Know the style rule.