Introduction

Setup

Operators

variable

String

Number

Dtype & Comment

Lists

Tuples

Sets

Dictionaries

if else statement

For Loop

While Loop

iterator & iterable

Pre-Built functions

Function

Decorators

Generators

Types of errors

Exceptions Handling

OOP

Regular expression

Create module

OS module

Date module

Random module

Math

Statistics module

Exercise 1

Exercise 2

Exercise 3

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Types of errors occurs in python and how to handle those errors.

SyntaxError error

Invalid syntax error raises when we do a mistake to write python syntax.

Example 1:
Input
for num in range(1,10)
  print(num)
Output
SyntaxError: invalid syntax

In the example, we don't write colon while creating for loop and it's a syntax mistake. So we get an invalid syntax error.

Example 2:
Input
i=7 if i==10
  print(num)
Output
SyntaxError: invalid syntax

In the example, we don't write colon while creating if condition and it's a syntax mistake. So we get an invalid syntax error.

IndentationError error

Suppose we wrote for loop or function or if statement. We know that after the "for" or "if" or "def" declaration line when we wrote the second line then we have to give space before writing the second line. If we forget to give this space then we will get an indentation error.

Example 1:
Input
for num in range(1,10):
print(num)
Output
IndentationError: expected an indented block

In the example 1, here while creating the for loop we don't space before print function. If we don't do this then it is mistake and we will get indent error.

Example 2:
Input
v=10 if v==10:
print(num)
Output
IndentationError: expected an indented block

In the example 2, here while creating the if condition we don't space before print function. If we don't do this then it is mistake and we will get indent error.

NameError error()

Suppose we want to print c but we didn't define c. So in this case we will get a name error. So name error occurs when we try to use a thing that is not defined.

Example:
Input
print(c)
print(func())
Output
NameError: name 'c' is not defined
NameError: name 'func()' is not defined

In the example, here we didn't create or define c variable and func() function and it is a mistake because if there is not things created then what the print function will print.

TypeError error

Type error occurs when we try to operate with the wrong type. Example: Suppose there two variables and you want to do sum of those values present in the variable. But the data types of those values are string. In this case if you try to summation of those variable values then you will get type error. To avoid the type error at first change the data type of those variable values into integer and then do summation.

Input
print(5+"num")
Output
TypeError: unsupported operand type(s) for +: 'int' and 'str'

In the example, we wanted to addition of a number and string and we got a type error because the type of number is int and the type of string is str.

IndexError error

Index error occurs when we try to perform work with the wrong index.

Example: Suppose you have a list which contain total 5 elements. Now you want to get the last element. So you write index number 5. But as we know the indexing starts in python from 1 and the last element index will be 4 but you passed 5. So you will an index error.

Example:
Input
li=[1,2,3]
print(li[4])
Output
IndexError: list index out of range

Here we have 3 elements in a list. So the index will start from 0 to 2. But we wanted to print 4 index values. Because there is no value at index 4, so we got an index error.

ValueError error

Value error occurs when we write correct data type but pass wrong data type value.

Example:
Input
s="aaa"
print(int(s))
Output
ValueError: invalid literal for int() with base 10: 'aaa'

Here we can put string value in s variable. We can also convert string to integer using int function. But we can't convert aaa into integer, so we got value error.

AttributeError error

Attributes error occurs when we try to use an attribute that doesn't exist.

Example: Suppose you have a list and you want remove the last value from the end of the list. To do this you will need pop function. But while using pop function you did a mistake while typing pop. In this you will get an attribute error.

Example:
Input
li=[1,2,3]
li.push("1")
Output
AttributeError: 'list' object has no attribute 'push'

Here we tried to use a function name push on a list, which doesn't exist. So we got an attribute error.

KeyError error

The key error occurs when we try to use the wrong key which doesn't exist or is defined.

Example:
Input
dic={"age":20,"name":"Rafsun"}
print(dic["price"])
Output
KeyError: 'price'

Here we tried to print the price from the dictionary. But there is no key named price. So we got the key error.

How to rise error in python?

In python you can raise your own error. Suppose you think a in your code some where an error can and the error can occur if the user put wrong input. In this you want to show the user a custom error so that the user can understand his or er mistake easily and can put or pass the write value. In these type of situation you can raise you own custom error. Yes, in python you can do this. Now let's see how to show or raise custom error in python.

Example:
Input
def add(x,y):
  if (type(x) is int) and (type(y) is int):
    return x+y
  raise TypeError("Sorry you are passing wrong type")
v=add(5,"2")
print(v)
Output
TypeError: Sorry you are passing wrong type

Here we tried to raise or display the error TypeError.

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.