Even experienced programmers make mistakes. Python groups these mistakes into three families. Knowing which type you are facing is the first step to fixing it.
| Type | When detected | Easy to spot? |
|---|---|---|
| Syntax error | Before the program runs | Yes — Python refuses to start |
| Run-time error | While the program is running | Yes — program crashes with a traceback |
| Logical error | Never (by Python) | Hardest — program runs but answer is wrong |
A syntax error means the code does not follow Python's grammar rules. Python cannot even begin running your program — it stops at the first bad line and shows SyntaxError.
File "prog.py", line 3 if x > 0 ^ SyntaxError: expected ':'
Three pieces of useful information: the file, the line number, and a caret pointing at the spot the parser got confused.
| Bad code | What's wrong |
|---|---|
if x > 0 | Missing colon at end |
print "hi" | Python 3 needs print() |
print("hi" | Unclosed parenthesis |
if x = 5: | Single = in a condition |
elseif x > 0: | Python uses elif, not elseif |
x = "hello | Unterminated string |
Python uses indentation to mark blocks (no curly braces). Wrong indentation gives IndentationError, which is a sub-type of syntax error.
if x > 0: print("positive") # NOT indented IndentationError: expected an indented block
print("start") on line 1 won't execute if there is a syntax error on line 5.A run-time error (also called an exception) happens while the program is running. The syntax was fine, but something illegal occurred at execution time. The program halts with a traceback.
| Exception | When it occurs |
|---|---|
ZeroDivisionError | Dividing by zero: 10 / 0 |
TypeError | Wrong type for an operation: "5" + 3 |
ValueError | Right type, bad value: int("hello") |
NameError | Variable used before defined: print(z) |
IndexError | List index out of range: L[10] when len = 3 |
KeyError | Missing dictionary key: d["xyz"] |
AttributeError | Object has no such method: s.append(1) on string |
FileNotFoundError | Open a file that does not exist |
OverflowError | Number too large for its type |
a, b = 10, 0 print(a / b) # ZeroDivisionError print("5" + 3) # TypeError n = int("hello") # ValueError print(z) # NameError (z not defined) L = [10, 20, 30] print(L[5]) # IndexError d = {"a": 1} print(d["b"]) # KeyError
TypeError means the kinds don't match (string + int). ValueError means the kind is right but the content is wrong (int("hello")). Easy to mix up — remember the difference.When a run-time error occurs, Python prints a multi-line message called a traceback. Read it from bottom to top — the last line is the most useful.
Traceback (most recent call last): File "prog.py", line 7, in <module> avg = total / count ZeroDivisionError: division by zero
You can't always avoid them, but a few small habits help a lot:
int(input()) only when you are sure the user will type a number.if 0 <= i < len(L): before L[i]; if k in d: before d[k].try / except when an error is expected and recoverable (covered in Class XII).a = int(input("a: ")) b = int(input("b: ")) if b == 0: print("Cannot divide by zero") else: print(a / b)The if-check converts a possible
ZeroDivisionError crash into a friendly message.A logical error means the code runs without crashing, but the answer is wrong. Python cannot detect these — only the human checking the output can.
a, b, c = 4, 6, 8 avg = a + b + c / 3 print(avg) # 12.666... (wrong) # Expected (4+6+8)/3 = 6.0The bug: precedence.
/ runs before +, so it computes a + b + (c/3). Fix with brackets: (a + b + c) / 3.| Pattern | Cause |
|---|---|
| Off-by-one | range(1, n) instead of range(1, n+1) |
| Missing brackets | Wrong operator order, as above |
| Wrong operator | % instead of // (or vice versa) |
| Wrong condition | > when you meant >= |
| Variable reuse | Forgot to reset a counter inside a loop |
| Swapped variables | Used length where you needed breadth |
total = 0 for i in range(1, 10): total += i print(total) # Expected: sum of 1..10 = 55 # Got: sum of 1..9 = 45 # Fix: range(1, 11)
| Aspect | Syntax | Run-time | Logical |
|---|---|---|---|
| Found by | Python parser | Python interpreter | Programmer |
| When | Before run | During run | After run |
| Program runs? | Never starts | Crashes mid-way | Runs fully |
| Output produced | None | Partial + traceback | Wrong answer |
| Easy to fix | Yes | Sometimes | Hardest |
| Example | if x > 0 | 10 / 0 | a + b / 2 |
| Python message | SyntaxError | Specific exception | None |
Most one-mark questions on this topic are of the form "State the type of error in the following code". Use the diagnosis flow above and look at the error message Python would produce:
| Error message starts with | Type |
|---|---|
SyntaxError / IndentationError | Syntax |
ZeroDivisionError, TypeError, NameError, ValueError, IndexError, KeyError, AttributeError | Run-time |
| (no message — just wrong output) | Logical |
int("hello") raises which error?
"5" + 3 raises:
For each snippet, write the output if it runs, or the name of the error it raises.
a = 10
print(a + b)
Output / error: L = [1, 2, 3]
print(L[3])
Output / error: x = 7
y = 0
print(x % y)
Output / error: d = {"a": 1, "b": 2}
print(d["c"])
Output / error: n = int("3.14") print(n)Output / error:
a, b, c = 2, 4, 6
avg = a + b + c / 3
print(avg)
Output / error: s = "hello" s.append("!") print(s)Output / error:
total = 0 for i in range(1, 5): total += i print(total)Output / error:
Each snippet has exactly one error. Write the line number, the error type (syntax / run-time / logical), and the corrected line.
1 age = 18 2 if age >= 18 3 print("adult")Line: Type: Correction:
1 L = [10, 20, 30] 2 for i in range(0, 4): 3 print(L[i])Line: Type: Correction:
1 marks = [88, 76, 92]
2 total = marks[0] + marks[1] + marks[2]
3 avg = total / 2
4 # Wanted: average of 3 marks
Line: Type: Correction: 1 num = int(input("N: ")) 2 print("You typed " + num)Line: Type: Correction:
1 s = "Python" 2 print(s.upper)Line: Type: Correction:
1 d = {"x": 1, "y": 2}
2 print(d["z"])
3 # Wanted: print value for x or y
Line: Type: Correction: TypeError and ValueError with one example each.
Each program should run without crashing on common inputs and produce a clear output. Use a separate sheet if you need more space.
a / b. If b is 0, print the message "Cannot divide by zero" instead of crashing.
a, b, c = 4, 6, 8 avg = a + b + c / 3 print("Average:", avg)
marks = {"a": 88, "b": 76, "c": 92} and print its value, or print "Key not found" if the key is missing. Use the in operator.
n = 10 total = 0 for i in range(1, n): total += i print("Sum:", total)
(i) if x > 0 print("+") (ii) print(10 / 0) (iii) area = length + breadth # meant l*b