Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 06 / Topic 2 of 10

Reading a traceback

When an exception is not handled, Python prints a traceback. Read it from the bottom up.

Real-world example: an average-marks function gets an empty list.

def average(marks):
    return sum(marks) / len(marks)

print(average([]))
Traceback (most recent call last):
  File "report.py", line 4, in <module>
    print(average([]))
          ~~~~~~~^^^^
  File "report.py", line 2, in average
    return sum(marks) / len(marks)
           ~~~~~~~~~~~^~~~~~~~~~~~
ZeroDivisionError: division by zero
  1. Last line: the exception type and message โ†’ ZeroDivisionError: division by zero.
  2. Line above it: the exact line that failed (line 2, inside average).
  3. Top lines: how we got there (line 4 called average).

๐Ÿ” Going deeper: the ~~~^^^ markers (Python 3.11+) point at the exact part of the line that failed. "most recent call last" means the newest call is at the bottom, which is why you read upwards.

Done here? The practice set for Part 6 โ€” 7 exercises with solutions โ€” is on the part page.

Search the guide