Three failure families — syntax, runtime and logical — plus the six runtime exceptions every ICS student must recognise on sight.
01
Learning objectives
Classify an error from a code snippet as syntax, runtime or logical.
Match NameError, TypeError, ValueError, IndexError, KeyError and ZeroDivisionError to the mistakes that raise them.
Explain why logical errors are the hardest to catch (no crash, no message).
Predict which line raises first when several suspicious lines exist.
Fix common runtime errors with one-line changes.
02
Lesson notes
Errors come in three families, and telling them apart is the first debugging skill. A syntax error breaks Python's grammar — a missing colon, an unclosed bracket — and the program never starts; the shell points at the offending line before running anything. A runtime error (an exception) passes the grammar check but explodes mid-run: dividing by zero, indexing past the end. A logical error is the sneakiest: the program runs to completion and prints a confidently wrong answer.
Learn the runtime gallery by trigger. NameError: a name that does not exist — usually a typo like print(nam) instead of name. TypeError: incompatible types combined, as in '5' + 5. ValueError: the type is acceptable but the value is impossible — int('abc') or int('3.5'). Notice how precisely the messages describe the offence; read them fully before touching the code.
Collection mistakes get their own pair. IndexError: a list position that does not exist — [1, 2, 3][3] asks for a fourth element. KeyError: a dictionary key that was never added — marks['Umar'] on a dictionary without Umar. And the arithmetic classic, ZeroDivisionError: 10 / 0, or dividing by a variable that happened to hold 0. Each of the six has a signature phrase you will meet again in tracebacks and in MCQs.
Logical errors get no message at all — the program's own confidence is the trap. average = sum(marks) // len(marks) runs perfectly and prints 85 instead of 85.67, because integer division threw away the fraction. The only defences are testing against hand-computed answers and careful tracing. That is why examiners call logical errors the hardest class: no traceback, no line number — just a wrong number wearing a right-looking program.
Board tip: MCQs give four lines and ask which raises TypeError — decide by asking what type each operand is, not what the line means. Second favourite: syntax or runtime? If Python refuses to start, it is syntax; if it dies part-way after printing earlier lines, it is runtime. Practise by deliberately breaking tiny programs and reading exactly what Python says in reply.
03
Worked code examples
Names and positions
The commented lines are the accidents: a misspelled name and an index one past the end. The running lines show the corrected forms.
name = "Ahmed"
# print(nam) # NameError: 'nam' is not defined
print(name)
nums = [1, 2, 3]
# print(nums[3]) # IndexError: list index out of range
print(nums[2])
Output
Ahmed
3
Types, values and zero
int('5') converts before adding, so the types agree; dividing by 2 instead of 0 avoids ZeroDivisionError and yields the float 5.0.
# total = "5" + 5 # TypeError: can only concatenate str to str
total = int("5") + 5
print(total)
# average = 10 / 0 # ZeroDivisionError: division by zero
average = 10 / 2
print(average)
Output
10
5.0
Missing keys handled, not crashed
Indexing a dictionary by a key it lacks raises KeyError; get() supplies a fallback value instead, keeping the program alive.