The Different Types of Errors in Python and How to Fix Them?


As a beginner in Python, you may encounter errors when trying to run your code. Don’t worry! We all make mistakes.

In this blog post, we will go over the main types of errors that you are likely to encounter as a beginner Python programmer. These include syntax errors, runtime errors, Logic Errors, and semantic errors. If you can learn to recognize these error types, you will be well on your way to becoming a master Python programmer!

Syntax Errors

Syntax errors are usually the first type of error you will encounter when writing code. These errors occur when the Python interpreter encounters code that it cannot parse because it does not adhere to the correct syntax for the language.

For example, take the following code:

print("Hello world!" # this line prints a string
Code language: PHP (php)

If you forget to include the opening and closing quotation marks around the string, like so:

>>> print "Hello world!") # this line prints a string File "<stdin>", line 1 print "Hello world!") # this line prints a string ^ SyntaxError: unmatched ')' # missing quotation marks
Code language: PHP (php)

Then you will get a syntax error. Syntax errors are usually easy to fix, once you know what they are. Simply find the line of code that is producing the error and check your syntax.

Make sure you are using the correct characters, and that everything is in the right order. If you’re still unsure, consult a Python reference guide or ask a friend for help.

Runtime Errors

Runtime errors occur while your program is running.

That is, they occur after your code has been successfully parsed by the Python interpreter but fails to execute properly.

For example, say you have written a function to calculate the average of a list of numbers:

def average(numbers): # calculates the average of a list of numbers total = 0 # initialize total to 0 for n in numbers: # iterate over each number in numbers total = total + n # add n to total return total / len(numbers) # return total divided by length of numbers
Code language: Python (python)

If you try to call this function with an empty list (i.e., average([])), then you will get a runtime error because you are trying to divide by 0 (the length of an empty list is 0).

numbers = [] x = average(numbers) print(x)
Code language: PHP (php)

To fix this type of error, you need to anticipate cases where your code might receive invalid input, and write additional code to deal with those cases gracefully.

In this particular example, one way to do that would be to check whether the length of the list is 0 before trying to divide by it:

def average(numbers): # calculates the average of a list if len(numbers) == 0: print("Error: Cannot calculate average of empty list.") return None else: total = 0 # initialize total to 0 for n in numbers: # iterate over each number in numbers total = total + n # add n to total return total / len(numbers) # return total divided by length of numbers
Code language: Python (python)

Now if someone tries to call your function with an empty list, they will see an informative error message instead of getting an ugly runtime error.

Handling invalid input like this is an important part of writing robust code.

Logic Errors

Logic errors occur when the program runs without crashing but does not produce the expected results.

For example, consider the following code:

x = 5 if x > 10: print('x is less than 10') else: print('x is greater than or equal to 10')
Code language: Python (python)

In this code, we are expecting the output to be “x is less than 10” since x equals 5—which is less than 10.

However, because we put the > symbol instead of <, our program will output “x is greater than or equal to 10.”

To fix this kind of error, you need to examine your code carefully to find out why it’s not giving you the expected results.

Semantic Errors

These are often called bugs and are usually more difficult to spot and fix than syntax or logic errors.

Semantic errors occur when there is a mistake in your program that doesn’t prevent it from running but produces incorrect results.

For example, consider the following code:

def calculate_total(x): total = x + x*0.1 # bug here! should be 0.2 not 0.1 return total calculate_total(100) # should return 120 but returns 110
Code language: Python (python)

In this code, we have a semantic error because we accidentally calculated 10% instead of 20%.

As a result, our calculation is off by 10%.

Fixing this kind of error requires tracing through your entire program to find where things went wrong and then correcting your mistakes accordingly.

As a Python programmer, it’s important to have a solid understanding of all four main types of errors that can occur in Python programs: syntax errors, runtime errors, logic errors, and semantic errors.

By familiarizing yourself with these different types of errors, you can save yourself a lot of time and frustration when something goes wrong in your code.

Thanks for reading!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts