In this tutorial, we’ll learn about errors in Python.
You’ve likely seen some errors as long as you’ve practiced Python. It’s time to learn more about them. This is extremely important because making mistakes and correcting them is the way to learn and improve!
At the end of this lesson, you’ll learn:
- What are the main Python error types
- General steps to handle errors
Let’s get started!
Python error types
There are two main Python error types: syntax errors and exceptions. We’ll talk about them one by one.
The syntax errors are also called parsing errors. They happen when our code is parsed/processed by Python to assess for syntax correctness. So before the code even gets executed, Python will return these error messages if there are basic syntax mistakes in our code. The typical syntax errors include missing symbols (like brackets and commas) and incorrect indentations.
Here are some examples of syntax errors. Python outputs the line of code with the error and displays a small arrow pointing at the earliest point where the issue was detected. This helps us to identify the error and fix it quickly.
In the above example, we are missing the closing bracket, so such
SyntaxError is returned.
This one is straightforward, too. We are missing the colon of the
Different from syntax errors, exceptions are errors detected during code execution. There are different exception types depending on the cause of the error. Some of the common built-in Python exceptions are
NameError. If you are interested, look at the built-in list of exceptions here.
Here are some examples.
In the above example, we can’t sum together an integer and a string. So Python returns a
TypeError exception. The
TypeError occurs when an operation or function is applied to an object of inappropriate type. And you can see the detailed explanation of the type mismatch after the
When we are trying for an index outside the object’s range, Python returns
Since we haven’t defined an object named
variable1, Python throws a
There are no functions or methods from the
pandas package named
read_file. So Python tells us it’s an
ImportError that we can’t import it.
Besides looking at exceptions to correct errors, we can do more things with them.
For instance, exceptions are not ‘unconditionally fatal’, meaning we can write code to handle exceptions without Python throwing an error message. You can read more about handling exceptions.
Plus, we can raise or define exceptions. It is useful when you get into more advanced coding of Python. This is also why there are so many types of exceptions – each Python package could define its own exceptions as well.
How to handle errors
By now, we’ve only looked at errors that are very straightforward to fix. You probably have run into error messages that are harder to understand. Let’s talk about some common steps of fixing them with an example.
In this example below, we want to sum up the values within the dictionary, which should be
1+2+3. However, this code returns a
Step #1: Read the error messages
The first step is always to read the error messages. When you just started coding in Python, you tend to miss them. They could look scary and confusing. But this is critical since Python does communicate the errors with us and provides hints within these messages.
In our example, it says it’s a
TypeError. As we’ve learned earlier, this is when we provide an operation or function unsupported types of objects. And more specifically, it’s saying for the operation
+=, we can not do integer and tuple.
Step #2: Research and read the examples and documents
This step is optional. If you have experience with this topic, you would know the problem based on the error messages. You can tell that the object
num is a tuple, which can’t be used for the operations
But let’s assume that you just started and are unfamiliar with all these. We can always research the error messages.
This time I would turn to our good friend Google.
Here is the featured snippet returned by Google. This blog post clearly explains a similar error message and how to solve the error. We need to figure out how the variable got assigned a tuple.
This is very helpful. So no matter what confusing error messages you have got, you should try searching it. You’ll be surprised to find that you are not alone most of the time. And perhaps other people have run into similar issues and posted solutions you can explore!
Step #3: Find the error source by breaking down the code
So let’s go back to the code and see how we assigned a tuple to the operation.
We only have one variable in operation, which means
num is the tuple.
num a tuple? We can look back to where we’ve assigned it.
for num in d.items():
d.items gives a tuple.
We can print it out and see that it returns tuples inside.
Or we can print
num inside the
for loop. If we run the code below, you can see that the code prints the first
num within the loop before stopping by the error, which is a tuple
So we must make sure
num only extract the values within the dictionary, which are the integers 1, 2, and 3.
There are different solutions. Try some of them below.
In reality, fixing errors could need more trials and errors. But as you gain experience, it should become easier!
At the end of this lesson, I would like to mention other errors. Besides the errors that will interrupt the Python code, there are also logical errors that run successfully but return unexpected results. For example, an incorrect formula is used for calculation, a wrong function is used for operations. So don’t forget to double-check some examples to ensure the results are what you want.
In this tutorial, you’ve explored Python errors and how to solve them. Hope now you are confident to practice and learn Python continuously!