ExampleEdit & Run
print(1 + "hello")
copy
Output:
Traceback (most recent call last):   File "<string>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' [Finished in 0.010380567982792854s]

TypeError exceptions occur on an attempt to perform an operation on objects of an inappropriate/unsupported type. This typically occurs when attempting to perform a type-specific operation on objects of the wrong type.

ExampleEdit & Run
#with operators
>>> 1 + "hello"
TypeError: unsupported operand type(s) for +: 'int' and 'str'

#with builtin functions
>>> int([1, 2, 3])
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
>>> float((1))
TypeError: float() argument must be a string or a real number, not 'tuple'
copy

Common causes of TypeError exceptions

Some of the most common causes for TypeError in Python are:

  • Performing an operation with unsupported types

ExampleEdit & Run
"hello" * "world!"
copy
Output:
Traceback (most recent call last):   File "<string>", line 1, in <module> TypeError: can't multiply sequence by non-int of type 'str' [Finished in 0.009996089152991772s]
  • Calling uncallable object

ExampleEdit & Run
demo = "Pynerds"

demo()
copy
Output:
Traceback (most recent call last):   File "<string>", line 3, in <module> TypeError: 'str' object is not callable [Finished in 0.010116863064467907s]
  • Incorrect type during indexing

ExampleEdit & Run
L = [1, 2, 3, 4, 5, 6]

print(L[1.0])
copy
Output:
Traceback (most recent call last):   File "<string>", line 3, in <module> TypeError: list indices must be integers or slices, not float [Finished in 0.009765449911355972s]
  • Iterating on an object that does not support iteration

ExampleEdit & Run
demo = 3.142
for i in demo:
    print(i)
copy
Output:
Traceback (most recent call last):   File "<string>", line 2, in <module> TypeError: 'float' object is not iterable [Finished in 0.009235456120222807s]

Avoiding and Handling TypeError Exceptions

Ensuring that the objects being used in an operation are of the appropriate types is the first step to avoid the TypeError exceptions. For example we can check in advance whether the values are of the correct type using the if block. 

ExampleEdit & Run
def add(a, b):
   """This function only works with integers and floating point values"""

   if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):

       return "The values entered are invalid"

   return "%s + %s = %s"%(a, b, a + b)

print(add(10, 20))

print(add(50, 60))

print(add(10, "Pynerds"))
copy
Output:
10 + 20 = 30 50 + 60 = 110 The values entered are invalid [Finished in 0.009712859988212585s]

In the above case, the TypeError exception is not raised because we explicitly avoid it. However, we can also use the try-except block to catch the TypeError when it is raised and proceed accordingly.

ExampleEdit & Run
def add(a, b):
   """This function only works with integers and floating point values"""

   try:

     return "%s + %s = %s"%(a, b, a + b)

   except:
       return "The values entered are invalid"

print(add(10, 20))

print(add(50, 60.5))

print(add(10, "Pynerds"))
copy
Output:
10 + 20 = 30 50 + 60.5 = 110.5 The values entered are invalid [Finished in 0.00921828718855977s]