Type() and isinstance() in Python | Python for Data Science | Day 15
Hello, Team Python!
The Data Monk is back with wholesome content on type() & isinstance() in Python.
We will discuss type() & isinstance() in Python with the help of various code samples. So, stay with us for the rest of the article, as we make it lucid for you!
The type() function has one required parameter i.e. the object. The function returns the class type of that object. Since Python is an OOP language, everything in Python is an object, pretty much obvious from the picture. Let us learn more about it with the help of code. Go through the examples given below:
In the first instance, type returned is str as “the data monks” is a string.
Let’s try another example.
Since 10 is an integer, int type is returned. Moving onto the next one.
Good. 4.67 is indeed a floating point number.
Now, let’s try it with a list.
The type() function does its job!
Now, let’s try a dictionary.
Okay! Dict type is returned as “b” is a dictionary.
So, now we know that type() returns the right existing class for the objects. But, what if we try it on a custom class – a class we create on our own. Do you think Python will recognize an object of that class? Let’s figure out!
First off, let’s begin by creating a class called “student”.
That’s the class we have created. It consists of the student’s name, grade, school, marks and subjects.
Now, let’s create an object of this class. The syntax to create an object of the class is very simple. Check this below:
We know that s is an object of type student. So, ideally, type() should return “student”. Let’s check if it does so.
As we expected, the type returned is “student” indeed. It is declared in main. Hence type() returns _main_.student .
Now, let’s check the type of the constituents of the student object – s.
So, the student’s name is a string.
Student’s grade is an integer.
Student’s school is a string.
Student’s marks are float.
Student’s subjects are of the type list.
So, with this, we are done with the type() function. Now, let’s move to our second topic i.e. isinstance() function.
Basically, type() and isinstance() are used in similar contexts. So, if you need to check if “Hey” is really a string or not, you use isinstance(). So, we use type() to find the type and isinstance to see if it belongs to the type you have provided.
Again, let’s consider some examples to understand the isinstance() function.
Carefully note the syntax. The first argument to isinstance() is an object whose type is to be matched to the second argument.
Since a is a string, isinstance() returns True.
Since b=2.7 is a float, the isinstance returns a False as the second argument passed to it is an int.
Let’s try it on our custom class now.
Since s is an instance of student class, the isinstance() function returns True.
We end the tutorial on this note. But, before that, we have some questions for you.
If you are stuck, go through our solutions.
We have covered 40+ complete Data Science company interviews from the candidates who cracked these interviews.
Data Science Companies interview questions
We also have 30+ e-books on Amazon, Insta Mojo and books which can be delivered directly on your email address
Complete Set of e-books from The Data Monk
Understand some of the very complex topics in Analytics which are asked in most of the interviews
The Data Monk Top Articles
How to become a Data Scientist? Complete study material, free resources and websites to practice
Become a Data Scientist
Make your profile on our website and practice at least 5-7 questions per day. Be a part of ~2000 Analytics expert
Intern | The Data Monk