Open In App
Related Articles

Handling TypeError Exception in Python

Improve Article
Save Article
Like Article

TypeError is one among the several standard Python exceptions. TypeError is raised whenever an operation is performed on an incorrect/unsupported object type. For example, using the + (addition) operator on a string and an integer value will raise a TypeError.


The general causes for TypeError being raised are:

1. Unsupported Operation Between Two Types

In the following example, the variable ‘geek’ is a string, and the variable ‘num’ is an integer. The + (addition) operator cannot be used between these two types and hence TypeError is raised.


geek = "Geeks"
num = 4
print(geek + num + geek)

Output :

TypeError: must be str, not int

2. Calling a non-callable Identifier

In the below example code, the variable ‘geek’ is a string and is non-callable in this context. Since it is called in the print statement, TypeError is raised.


geek = "GeeksforGeeks"

Output :

TypeError: 'str' object is not callable

3. Incorrect type of List Index

In Python, list indices must always be an integer value. Since the index value used in the following code is a string, it raises TypeError.


geeky_list = ["geek", "GeeksforGeeks", "geeky", "geekgod"]
index = "1"

Output :

TypeError: list indices must be integers or slices, not str

4. Iterating Through a non-iterative Identifier

In the following code, the value 1234.567890 is a floating-point number and hence it is non-iterative. Forcing Python to iterate on a non-iterative identifier will raise TypeError.


for geek in 1234.567890:

Output :

TypeError: 'float' object is not iterable

5. Passing an Argument of the Wrong Type to a Function

In the below code, subtraction function performs difference operation between two arguments of same type. But while calling the function if we pass arguments of two different type then Type error will be thrown by interpreter.


def subtraction(num1, num2):
subtraction('a', 1)


Hangup (SIGHUP)

Traceback (most recent call last):
File "", line 5, in <module>
subtraction('a', 1)
File "", line 2, in subtraction
TypeError: unsupported operand type(s) for -: 'str' and 'int'

Handling TypeError

TypeErrors are raised mostly in situations where the programmer fails to check the type of object before performing an operation on them. They can be handled specifically by mentioning them in the except block. In the following example, when one of the indices is found to be an incorrect type, an exception is raised and handled by the program.


geeky_list = ["Geeky", "GeeksforGeeks", "SuperGeek", "Geek"]
indices = [0, 1, "2", 3]
for i in range(len(indices)):
    except TypeError:
        print("TypeError: Check list of indices")

Output :

TypeError: Check list of indices

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 04 Sep, 2023
Like Article
Save Article
Similar Reads
Related Tutorials