CodingNic

Error Handling and Debugging

Common Python Errors

Error Handling and Debugging 28 min read

Common Python Errors

Common Python Errors

Every programmer sees errors.

Errors are normal.

The important skill is learning what they mean and how to fix them.

In this lesson, you will learn common Python errors beginners often face.

Why Learn Errors?

When you recognize an error quickly, you can fix it faster.

This saves time and builds confidence.

1. SyntaxError

Happens when Python cannot understand your code.

Example:

python
if True
    print("Hello")

Problem

Missing colon :.

Fix

python
if True:
    print("Hello")

2. NameError

Happens when using a variable that does not exist.

Example:

python
print(score)

Fix

Create the variable first.

python
score = 90
print(score)

3. ValueError

Happens when the value type is wrong.

Example:

python
int("hello")

Fix

Use a valid number string.

python
int("5")

4. TypeError

Happens when using wrong data types together.

Example:

python
"5" + 2

Fix

Convert types first.

python
int("5") + 2

Output

text
7

5. ZeroDivisionError

Happens when dividing by zero.

python
10 / 0

Fix

Check before dividing.

python
number = 0

if number != 0:
    print(10 / number)

6. IndexError

Happens when list index does not exist.

python
items = ["Pen", "Book"]
print(items[5])

Fix

Use a valid index.

python
print(items[1])

7. FileNotFoundError

Happens when opening a missing file.

python
open("missing.txt")

Fix

Check the file name or use try/except.

Tips for Fixing Errors

  • Read the message carefully
  • Check line numbers
  • Check spelling
  • Check brackets and colons
  • Use print() or debugger tools
  • Fix one error at a time

Code Along

Create one example that causes NameError.

Then fix it.

Mini Challenge

Fix this code:

python
age = "20"
print(age + 5)

Expected output:

text
25

Real World Use Case

Developers solve errors every day while building apps, websites, data tools, and automation scripts.

Quiz

  1. What causes SyntaxError?
  2. What causes NameError?
  3. What causes TypeError?
  4. What causes IndexError?

Assignment

Create examples of 3 different Python errors, then fix them.

Summary

You learned common Python errors, what causes them, and how to solve them faster.