Sitemap
Kranthikiran

Kranthikiran

Python Errors Aren’t Our Enemy:

Understanding Errors and Exceptions in Python

3 min readAug 14, 2026

--

Introduction

When I started learning Python, one thing I noticed was that errors come up pretty often. Even a small mistake in the code can give us an error message. At first, these errors can be confusing, but actually they are useful because they tell us what is wrong with our program.

Instead of being afraid of errors, we should understand what they mean and learn how to fix them.

What is an Error in Python?

An error is a problem in a program that makes it work incorrectly or stops it from running.

For example:

print(“Hello”

Here, the closing bracket is missing, so Python gives an error.

Errors can happen because of wrong syntax, incorrect data types, missing variables, or invalid operations.

Common Python Errors

1. SyntaxError

A “SyntaxError” occurs when we don’t follow Python’s syntax correctly.

Press enter or click to view image in full size

Correct code:

2. NameError

A “NameError” occurs when we use a variable that hasn’t been defined.

name = “Kranthi”

print(nam)

Here, “nam” is different from “name”, so Python doesn’t know what “nam” means.

3. TypeError

A “TypeError” occurs when we use incompatible data types.

age = 21

print(“Age: “ + age)

Here, a string and an integer are being combined. We can fix it using “str()”:

print(“Age: “ + str(age))

4. ValueError

A “ValueError” occurs when a value is not suitable for an operation.

number = int(“hello”)

“”hello”” cannot be converted into an integer, so Python gives a “ValueError”.

Get Kranthikiran’s stories in your inbox

Join Medium for free to get updates from this writer.

5. ZeroDivisionError

This occurs when we try to divide a number by zero.

print(10 / 0)

Python gives a “ZeroDivisionError” because division by zero is not possible.

6. IndexError and KeyError

An “IndexError” occurs when we try to access an index that doesn’t exist.

numbers = [10, 20, 30]

print(numbers[5])

A “KeyError” occurs when we try to access a dictionary key that doesn’t exist.

student = {“name”: “Kranthi”}

print(student[“marks”])

Handling Errors Using try and except

Python allows us to handle exceptions using “try” and “except”.

try:

number = int(input(“Enter a number: “))

print(number)

except ValueError:

print(“Please enter a valid number”)

If the user enters something that is not a number, the program will not suddenly stop. Instead, it will show a useful message.

Why Are Errors Important?

Errors are a normal part of programming. Even experienced programmers make mistakes.

Understanding errors helps us:

- Find mistakes faster

- Debug our programs

- Understand what went wrong

- Prevent programs from crashing

- Improve our programming skills

Conclusion

Python errors may look scary at first, but they are actually helpful. They give us clues about what is wrong with our code.

Errors such as “SyntaxError”, “NameError”, “TypeError”, “ValueError”, “IndexError”, “KeyError”, and “ZeroDivisionError” are common while learning Python.

Once we understand these errors and learn how to handle exceptions using “try” and “except”, debugging becomes much easier.

So instead of thinking “Oh no, an error!” every time we see one, we can think of it as Python telling us what we need to fix.

--

--