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 TypeError. Show ExamplesThe 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. Python3
TypeError: 'str' object is not callable0 TypeError: 'str' object is not callable1 TypeError: 'str' object is not callable2 TypeError: 'str' object is not callable3 num TypeError: 'str' object is not callable3 TypeError: 'str' object is not callable6 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. Python3
TypeError: 'str' object is not callable9 TypeError: 'str' object is not callable1 TypeError: list indices must be integers or slices, not str1 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. Python3TypeError: list indices must be integers or slices, not str2 = TypeError: list indices must be integers or slices, not str4 TypeError: list indices must be integers or slices, not str5 TypeError: list indices must be integers or slices, not str6 TypeError: 'str' object is not callable9 TypeError: list indices must be integers or slices, not str6 TypeError: list indices must be integers or slices, not str9 TypeError: list indices must be integers or slices, not str6 TypeError: 'float' object is not iterable1 TypeError: 'float' object is not iterable2 TypeError: 'float' object is not iterable3 = TypeError: 'float' object is not iterable5 TypeError: 'str' object is not callable1 TypeError: 'float' object is not iterable7 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. Python3TypeError: 'float' object is not iterable8 geek Geeky GeeksforGeeks TypeError: Check list of indices Geek0 Geeky GeeksforGeeks TypeError: Check list of indices Geek1 Geeky GeeksforGeeks TypeError: Check list of indices Geek2 Geeky GeeksforGeeks TypeError: Check list of indices Geek3 TypeError: 'str' object is not callable1 Geeky GeeksforGeeks TypeError: Check list of indices Geek5 Output : TypeError: 'float' object is not iterable Handling TypeErrorTypeErrors 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. Python3TypeError: list indices must be integers or slices, not str2 = TypeError: list indices must be integers or slices, not str4 Geeky GeeksforGeeks TypeError: Check list of indices Geek9 TypeError: list indices must be integers or slices, not str6 TypeError: 'str' object is not callable9 TypeError: list indices must be integers or slices, not str6 geek 3TypeError: list indices must be integers or slices, not str6 geek 5TypeError: 'float' object is not iterable2
TypeError: list indices must be integers or slices, not str4 = 0TypeError: list indices must be integers or slices, not str6 = 2TypeError: list indices must be integers or slices, not str6 = 4TypeError: list indices must be integers or slices, not str6 = 6TypeError: 'float' object is not iterable2 |