What will Python raise if the print line is not indented inside the function?
def greet(): print("Hello") # Python raises: [1]
Python raises IndentationError when a statement that should be inside a block is not indented. The print must be indented under def.
How many spaces does Python's PEP 8 standard recommend per indentation level?
# PEP 8 recommends [1] spaces per indent level def example(): if True: pass # this line is 2 levels deep
Python's PEP 8 style guide recommends 4 spaces per indentation level. The pass line is 2 levels deep so it uses 8 spaces total (2 Γ 4).
What does this code print? Pay close attention to which print belongs inside the loop.
for i in range(3): print("A") print("B") # Total lines printed: [1]
The indented print("A") runs 3 times inside the loop. The unindented print("B") runs once after the loop ends. Total output: 4 lines (A, A, A, B).
Fill both blanks. The return statement's indentation decides when the function returns.
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
The return is at the function level (one indent), not inside the loop. So the loop completes fully (1+2+3=6) before returning. If return were indented inside the loop, it would return 1 on the first iteration.
Fill all three blanks. Determine each output line based on which block each print belongs to.
x = 10 if x > 5: print("big") if x > 8: print("very big") print("done") # Line 1: [1] # Line 2: [2] # Line 3: [3]
Since x=10: the outer if x > 5 is true β prints big. The nested if x > 8 is also true β prints very big. The unindented print("done") always runs because it is outside all if blocks.