top of page

Data Scientist Program


Free Online Data Science Training for Complete Beginners.

No prior coding knowledge required!

Python Concepts for Data Science: Errors and Exceptions

Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal events occur which changes the normal flow of the program.

Two types of Error occurs in python

  1. Syntax errors

  2. Logical errors (Exceptions)

Syntax errors

When the proper syntax of the language is not followed then syntax error is thrown.


# initialize exam score
x = int(input("What is your exam score?"))
# check  whether the student has passed their exam or not 
    print("Congrats! you succeed ")
else print("You did not pass the exam")

this code returns a syntax error message because after if statement a colon : is missing.

logical errors(Exception)

Here’s a list of the common exception you’ll come across in Python:

  • ZeroDivisionError: raised when you try to divide a number by zero

  • IndexError: Raised when the wrong index of a list is retrieved.

  • AssertionErrorIt Raised when assert statement fails.

  • AttributeError Raised when an attribute assignment is failed.

  • ImportError: Raised when an imported module is not found.

  • KeyError Raised when the key of the dictionary is not found.

  • NameError Raised when the variable is not defined.

  • TypeError Raised when a function and operation is applied in an incorrect type.

  • MemoryError Raised when a program run out of memory.

  • ValueError: Raised when the built-in function for a data type has the valid type of arguments, but the arguments have invalid values specified

  • Exception: Base class for all exceptions. If you are not sure about which exception may occur, you can use the base class. It will handle all of them.

Example1: ZeroDivisionError

# initialize the amount variable
marks = 10000
# perform division with 0
a = marks / 0

Example2: Index Error

#initialize a list
a = [1, 2, 3] 
print (a[3]) 

Example3: AttributeError

class Attributes(object):
object = Attributes()
print (object.attribute)

Example4: ImportError

import module

Example5: KeyError

array = { 'a':1, 'b':2 }
print (array['c'])

Example6: NameError

def func():
    print (ans)

Example6: TypeError

arr = ('tuple', ) + 'string'
print (arr)

Example7: ValueError

print (int('a'))

Error Handling

When an error and an exception is raised then we handle them with the help of handling methods.

Handling Exceptions with Try/Except/Finally

We can handle error by Try/Except/Finally methods. We write unsafe code in the try, fall back code in except and final code in finally block.

# put unsafe operation in try block
     print("code start")
     # unsafe operation perform
     print(1 / 0)
# if error occur the it goes in except block
     print("an error occurs")
# final code in finally block

    a = 10/0  
    print (a)
except ArithmeticError as e:  
        print ("This statement is raising an arithmetic exception.")
    print ("Success.")

Thank you for regarding!

You can find the complete source code here Github


Recent Posts

See All

2 commentaires

Data Insight
Data Insight
21 sept. 2021


ben othmen rabeb
ben othmen rabeb
22 sept. 2021
En réponse à

Thank you! 😃

bottom of page