Skip to content
ComputerCS
Start learning

Python Programming · Lesson 2.9.1

Error Types

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.

marks = {"Ali": 88}
# print(marks["Umar"])    # KeyError: 'Umar'
print(marks.get("Umar", "absent"))
Output
absent
04

Key terminology

syntax error

A grammar mistake that stops the program before it starts.

runtime error

An exception raised while the program is running, crashing it at that line.

logical error

A flaw that lets the program run to the end but produce wrong results.

exception

Python's object signalling a runtime error, carrying a type and a message.

ValueError

Raised when a function gets a value of the right type but impossible meaning, like int('abc').

IndexError

Raised when a sequence is indexed past its last element.

05

Common misconceptions

Common mistake

If the program runs and prints something, it is correct.

Correct understanding

Logical errors produce confidently wrong answers with no warning message at all — only testing against known answers reveals them.

Common mistake

int('3.5') converts to 3.

Correct understanding

int() accepts only whole-number text; '3.5' raises ValueError. Convert with float('3.5') first, then int() if needed.

Common mistake

NameError means a variable has the wrong type.

Correct understanding

NameError means the name does not exist at all — usually a spelling mistake. Type mismatches raise TypeError.

06

Check your understanding

1. print('5' + 5) raises —

2. nums = [1, 2, 3]. Which line raises IndexError?

3. average = sum([80, 85, 90]) // 3 prints 85 instead of 85.67. This is a —

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.