Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Unsupported Operation between two objects
3.
Calling a non-callable object or function
4.
Incorrect type of lists index
5.
Iterating a non-iterative object
6.
Handling TypeErrors
7.
FAQs
8.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Handling TypeError exception in python

Author Nishant Rana
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

TypeError is one of the Exceptions in Python. Whenever we make a call or access any object which does not exist or is unsupported then the TypeError exception occurs. For example, if we try to multiply(*) two strings then the TypeError exception would occur.

Let us now see various reasons why this error occurs:-

Also Read About, Floor Division in Python and Convert String to List Python.

Unsupported Operation between two objects

If we apply an operation that is not supported between the two objects, then a TypeError exception occurs. For example, if we multiply two strings. We can only multiple integers, floats, etc but not strings.
 

Code

str1 = "Coding"
str2 = "Ninjas"
print(str1 * str2)

Output

TypeError: can't multiply sequence by non-int of type 'str'

 

You can also read about the Multilevel Inheritance in Python.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Calling a non-callable object or function

If we call a non-callable object then a TypeError exception occurs. For example, calling an integer value will give an error.

Code

value = 12
value()

Output

TypeError: 'int' object is not callable

Incorrect type of lists index

The list indices should be integer only. If we access them with a character, string, float, etc. then we will get the TypeError exception.

Code

values = [1, 2, 3, 4, 5]
print(values['1'])

Output

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

Recommended Topic, Divmod in Python

Iterating a non-iterative object

If we try to iterate a non-iterative object, then also we will get the TypeError exception. For example, iterating an integer value.

Code

values = [1, 2, 3, 4, 5]
print(values['1'])

Output

TypeError: 'int' object is not iterable

Check out this article - Quicksort Python

Handling TypeErrors

We can handle the TypeError using the ‘try’ and ‘except’ blocks. We can explicitly mention it in the except block.

Code

list = ["Coding", "Ninjas", "CodingNinjas"]
indices = [0, 1, "2"]
for i in range(len(indices)):
    try:
        print(list[indices[i]])
    except TypeError:
        print("TypeError: Check list of indices")

Output

Coding
Ninjas
TypeError: Check list of indices

 

You can try it on online python compiler.

Check out Escape Sequence in Python

FAQs

  1. What is a TypeError?
    TypeError is one of the Exceptions in Python. Whenever we make a call or access any object which does not exist or is unsupported then the TypeError exception occurs.
     
  2. How do we handle TypeErrors?
    We can handle the TypeError using the ‘try’ and ‘except’ blocks. We can explicitly mention TypeError in the except block.

Conclusion 

In this article, we have extensively discussed the following things:

  1. We first discussed what are TypeErrors and how they occur.
  2. Then we discussed how to handle TypeErrors.

We hope that this blog has helped you enhance your knowledge regarding TypeErrors in Python language and if you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow. Happy Coding!








 

Previous article
Handling NameError Exception in Python
Next article
Conditional Statements in Python
Live masterclass