isinstance() in Python | Python isinstance() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The isinstance() function in python returns True if the specified object is of the specified type; otherwise, it will return False. It states that when we pass an object and class, or tuple of class, to the isinstance function, if the object's data type is the same as the given class, it will return True; otherwise, False.

Syntax of isinstance() in Python

The syntax of the isinstance function in Python is:

Where object can be any data structure available in python, and classinfo is a class object or group of tuples.

Tip:

Tuple is one of the four inbuilt data types available in python. The tuple is enclosed in () round brackets. It helps us to store data in it. The tuple is similar to the list in terms of indexing; the data in the tuple are unchangeable.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Parameters of isinstance() in Python

object: The object that is required to check as a part of the class or not.

classinfo: The class , type, or tuple of classes and types required to compare with the object's class.

TIP:

If classinfo is not a type or tuple of types or any other type that does not match with classinfo, then the isinstance function will raise TypeError.**

Return Type of isinstance() in Python

Return Type: bool

This function returns a boolean value, i.e., True or False.

It will return True if the object class matches the classinfo class; otherwise, False.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Example of isinstance() in Python

Code:

Output:

In the above example, a list l1 is used as an input containing integer values.

So, we consider: object: l1 classinfo: list

Now, the isinstance() function will check whether the class of the object (i.e., list) is the same as classinfo's class (i.e., list), then it will return True else False.

What is the isinstance function in Python?

The isinstance() function can be used to check the data type of the specified object matches the specified class. It states that when we pass an object and classinfo to the isinstance function, this function compares the object's class with the classinfo class(es). If any class(es) matches, it will return True; otherwise, False.

Note:

The classinfo parameter contains either a single class or a tuple of classes. In the coming section of examples, we have used a variety of examples to show how to use multiple classes to use the isinstance function.**

More Examples of isinstance() function

Example 1: Python isinstance with int and list

Code:

Output:

Example 2: Use of isinstance() with objects

Code:

Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 3: Python isinstance with string

Code:

Output:

Example 4: Python isinstance with multiple classinfo

Code:

Output:

Example 5: Python isinstance with dict

Code:

Output:

Example 6: Python isinstance class methods

Code:

Output:

Conclusion

  • The isinstance() function in python takes 2 parameters i.e., object and classinfo.
  • Isinstance function returns a bool value, True if the object class is matched with classinfo, else False.
  • In the parameter classinfo, we can pass multiple classinfo using tuples.

See Also:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more