Account
Categories

Python Errors and Their Types


Definition:

When you write a program and run it, sometimes the code does not work.

It happens when a part of the program is incorrect.

For example: a syntax error, indentation error, type error, etc.

These problems cause errors and the program will not run. It will also display an error message.

Some types of errors in Python are:

1. Syntax Error

Definition:

It is an error that happens when you make a mistake in writing the program, and some part of the code is missing.

For example, if an if statement, parentheses, or other needed parts are missing. It is called a syntax error.

Example:

x =
print(x)

Output:

SyntaxError: invalid syntax

2. Runtime Error

Definition:

It is an error that may occur when you run the program.

Even if the program code is correct, an error can still present while the program is being executed line by line.

It is called the runtime error.

Example:

arr = [1, 2, 3]
print(arr[5])

Output:

IndexError: list index out of range

3. Logical Error

Definition:

It is an error that may occur when you run a program.

You use the wrong method, formula, or idea that produces the logic error.

The program runs, but the result is not correct.

Example:

a = 10
b = 5
print(a - b)

Output:

5