Type() and isinstance() in Python | Python for Data Science | Day 15

Hello, Team Python!
The Data Monk is back with wholesome content on Type() and isinstance() in Python in Python.
We will discuss Type() and isinstance() in Python 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!

type()

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:

Type() and isinstance() in Python

In the first instance, type returned is str as “the data monks” is a string.

Let’s try another example.

Type() and isinstance() in Python

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 constituents of the student object – s.

So, the student’s name is a string.

The student’s grade is an integer.

Student’s school is a string.

Students’ marks are float.

Students’ 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. 

isinstance()

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. Type() and isinstance() in Python

The Data Monk Interview Books – Don’t Miss

Now we are also available on our website where you can directly download the PDF of the topic you are interested in. At Amazon, each book costs ~299, on our website we have put it at a 60-80% discount. There are ~4000 solved interview questions prepared for you.

10 e-book bundle with 1400 interview questions spread across SQL, Python, Statistics, Case Studies, and Machine Learning Algorithms – Ideal for 0-3 years experienced candidates

23 E-book with ~2000 interview questions spread across AWS, SQL, Python, 10+ ML algorithms, MS Excel, and Case Studies – Complete Package for someone between 0 to 8 years of experience (The above 10 e-book bundle has a completely different set of e-books)

12 E-books for 12 Machine Learning algorithms with 1000+ interview questions – For those candidates who want to include any Machine Learning Algorithm in their resume and to learn/revise the important concepts. These 12 e-books are a part of the 23 e-book package

Individual 50+ e-books on separate topics

Important Resources to crack interviews (Mostly Free)

There are a few things which might be very useful for your preparation

The Data Monk Youtube channel – Here you will get only those videos that are asked in interviews for Data Analysts, Data Scientists, Machine Learning Engineers, Business Intelligence Engineers, Analytics Manager, etc.
Go through the watchlist which makes you uncomfortable:-

All the list of 200 videos
Complete Python Playlist for Data Science
Company-wise Data Science Interview Questions – Must Watch
All important Machine Learning Algorithm with code in Python
Complete Python Numpy Playlist
Complete Python Pandas Playlist
SQL Complete Playlist
Case Study and Guesstimates Complete Playlist
Complete Playlist of Statistics

Author: TheDataMonk

I am the Co-Founder of The Data Monk. I have a total of 6+ years of analytics experience 3+ years at Mu Sigma 2 years at OYO 1 year and counting at The Data Monk I am an active trader and a logically sarcastic idiot :)