Updated April 13, 2023
Introduction to Python TypeError
TypeError is a kind of error that python generates. We are trying to perform the operation of the wrong type of object. For example, if we are trying to do the square root of a number but we are passing a list instead of int, then TypeError will be generated by the python. This type of error is also generated when we are trying to perform the operation on the object, and it is not supported.
How to Avoid TypeError?
Python always checks the type of object we are passing for operation and whether a particular object type supports the operation. Python will throw a TypeError. We can avoid this error by adding an extra step or try-catch before such an operation. Suppose we want to join two lists. So before joining the operation, we can check the type of both the list that the user passes; if the type doesn’t match our requirement, we can pass a message to the user indicating wrong input and tell him to pass the proper input.
Examples of Python TypeError
Following are the examples are given below:
list1 = [7,8,9,10] list2 = 's'; result = list2.join(list1) print(result)
As you can see in the above program, we have created a list of list1 and list2 as a string variable. Now we are trying to join the list1 with list2, but python returns the type error. Because list2 is of string type, and python is expecting each element of the list1 to be a string, but it’s an integer.
list1 = ['7','8',9,10] list2 = 's'; result = list2.join(list1) print(result)
Now in the above, we have modified our first list, we have made the first two elements as a string, but the rest two elements are still int. So when we execute it, python still generates TypeError, as it says element index 2, i.e. the third element is still an integer. So now we have to make the rest two elements also as integers to work properly.
list1 = ['7','8',9,10] list2 = 's'; result = list2.join(str(a) for a in list1) print(result)
In the above program, you can see inside the join function, we have converted each element of list1 into a string by typecasting. It will make sure each element is a string; if not, then it will be covert it. So in this way, we can avoid TypeError.
a = 's'; b = 4; c = a/b; print(c)
In the above example, you can see that we have created two variables. One is holding an integer value, and another is holding a string or character value. Now we are trying to perform division between the variable and hold the result into the third variable and printing the result. But python will throw TypeError because an integer cannot be divided by string or character, the python was expecting integer value, so it throws TypeError.
Now in such cases, we know that we have to do division operation, and both variables should be an integer, so we have to handle it before performing such an operation.
a = 's'; b = 4; if(type(b) != int or type(a) != int): print('One of the number is not integer') else: c = a/b; print(c)
In this example, we can see that we have put the if condition checking the type of both the variables; if any of the variables is not an integer, then we are passing a message to the user to enter the values again. In this way, we can avoid TypeError.
a = 5; b = 4; if(type(b) != int or type(a) != int): print('One of the number is not integer') else: c = a/b; print(c)
This time our program worked correctly.
list1 = 's'; list2 = [3, 4, 5, 8, 9]; print(list1 + list2)
In the above example, you can; we are trying to contact two variables. One is a holding string, and the other is a holding list. Python returns a type error because the string cannot be concatenated to the list. Python is expecting a list, so it returns a type error. So to avoid this, we can check the type of variable before executing the operation.
list1 = 's'; list2 = [3, 4, 5, 8, 9]; if(type(list1) != list or type(list2) != list): print('One of the values is not list') else: print(list1 + list2)
In this example, we are checking the type of both the variables before contacting them; thus, we can avoid this type of error.
This is a guide to Python TypeError. Here we also discuss the introduction and how to avoid typeerror? Along with different examples and its code implementation. you may also have a look at the following articles to learn more –