Python Error Types

In the previous article, you have learnt about various programming paradigms. A program is instructions to the computer. If the instruction are not correct then it will result in error.

In this article, you will learn about different types of errors in programming and types of error that python programming language identifies.

Error in Programming

Error in programming is common and there are different categories of error. Some of them are easily identifiable and some are not. There are two major categories of errors.

  1. Compile time errors
  2. Run-time errors

The python programming language is a interpreted scripting language. It does not compile a program in to machine code. The interpreter executes the source code line by line. Therefore, a compile time error is any error found in the source code.

The run-time errors are those errors that are identified during the execution of program, means when the program is in memory.

Incorrect Output

There is a category of error which associated with outcome of a program. A program may run successfully, but give an incorrect result. These types of errors are very hard to find.

For example, if you want to compute average of 5 numbers, however, you divide the sum of all numbers by 6 (instead of 5). You may get an output, but that will be incorrect.

// Find average of following 
a = 10
b = 23
c = 17
d = 30
e = 25
average = (a + b + c + d + e)/6  
# This is a mistake in above code , the total items are 5
# This is an example of logical error or incorrect output

With incorrect results, the problem is in the logic of the program. Therefore, it is also a logical error in the programming.

We will discuss more about incorrect result, when we talk about debugging python programs.

Types of Error in Python

The python programming language recogize 4 types of error. The list is given below:

  1. Name Error
  2. Type Error
  3. Syntax Error
  4. Attribute Error

We will discuss each of them in detail.

Name Error

The name error occurs when you try to use a variable that does not exist in the program. The python will execute all the lines of code one-by-one until it encounters an error such as a name error.

Consider the following example where we attempt to use a unknown variable.

// Name error in Python
a = 20
b = 10
c = a + b + d
print(c)

The above code will generate following name error.

================= RESTART: C:/Python_projects/Name_Error.py =================
Traceback (most recent call last):
  File "C:/Python_projects/Name_Error.py", line 3, in <module>
    c = a + b + d
NameError: name 'd' is not defined

In the program above, the variable is not defined and hence, it becomes a name error.

Type Error

A type error happens in python when you try to do something not meant for the data type. For example, you cannot multiply two strings and use two numbers to multiply.

Consider the following example,

##Example of a Type Error
str1 = "Medical"
str2 = "Cure"
print(str1 * str2)
# The program above tries to multiply two strings and result in error

The output of the program is given below.

================= RESTART: C:/Python_projects/Type_Error.py =================
Traceback (most recent call last):
  File "C:/Python_projects/Type_Error.py", line 3, in <module>
    print(str1 * str2)
TypeError: can't multiply sequence by non-int of type 'str'

Syntax Error

The syntax error is the most common type of error in any programming language. It is related to the instruction that you give in a program. Each instruction must follow a grammar of the programming language.

For example, using a wrong keyword will result in syntax error

##The program prints an integer after conversion from string
str = "100"
myInt = int(str)
if myInt == 100
   print(myInt)
# The program prints integer but result in syntax error
# because the if statement does not end in 'colon'
# resulting in syntax error.

The result of the program is given below.

  File "<ipython-input-4-9ad9ea10cf9f>", line 3
    if myInt == 100
                   ^
SyntaxError: invalid syntax

The above code is written using Jupiter Notebook(Anaconda).

Attribute Error

Similar to type error, you can use specific properties of a data type. It you invoke wrong property of a data type. It will result in attribute error in python.

Consider following example.

str = "Apple"
num = 100
print(str.endswith("le"))
print(num.endswith("le"))
# The first print statement works because it is using the string
# The second print statement fails with Attribute error due to data type # being Number

The output of the above code is given below.

True

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-37f0e0108256> in <module>
      2 num = 100
      3 print(str.endswith("le"))
----> 4 print(num.endswith("le"))

AttributeError: 'int' object has no attribute 'endswith'

If you observe the code, the first print statement will result in True.That is, because the string endswith "le".

Skip to content