---Advertisement---

Python type() and isinstance() Functions Explained with Best Examples 2025

By Bhavani

Updated On:

---Advertisement---
 Python type() and isinstance() Functions

Python type() and isinstance() Functions: In Python, understanding data types is crucial for effective programming. The type() and isinstance() functions are built-in tools that help identify and verify data types. This guide explains these functions in detail with examples.

Python type() and isinstance() Functions:The type() function is used to determine the class type of a variable. It is commonly used for debugging, data validation, and dynamic coding.

The type() function can be used in two ways:

  1. type(object) – Returns the type of the object.
  2. type(name, bases, dict) – Creates a new type object.
  • object: (Required) The object whose type you want to identify.
  • name: (Required when using the second syntax) Specifies the name of the class.
  • bases: (Optional) Specifies the base class.
  • dict: (Optional) Specifies the namespace that defines the class attributes.
  • When passing one parameter, type() returns the data type of the object.
  • When passing three parameters, type() returns a new class object.

python

str_list = “Welcome to Python”

age = 50

pi = 3.14

c_num = 3j + 10

my_list = [“A”, “B”, “C”, “D”]

my_tuple = (“A”, “B”, “C”, “D”)

my_dict = {“A”: “a”, “B”: “b”}

my_set = {‘A’, ‘B’, ‘C’}

print(“The type is:”, type(str_list))

print(“The type is:”, type(age))

print(“The type is:”, type(pi))

print(“The type is:”, type(c_num))

print(“The type is:”, type(my_list))

print(“The type is:”, type(my_tuple))

print(“The type is:”, type(my_dict))

print(“The type is:”, type(my_set))

python

The type is: <class ‘str’>

The type is: <class ‘int’>

The type is: <class ‘float’>

The type is: <class ‘complex’>

The type is: <class ‘list’>

The type is: <class ‘tuple’>

The type is: <class ‘dict’>

The type is: <class ‘set’>

python

class Test:

    s = ‘testing’

t = Test()

print(type(t))

javascript

<class ‘__main__.Test’>

python

class MyClass:

    x = ‘Hello World’

    y = 50

t1 = type(‘NewClass’, (MyClass,), dict(x=’Python’, y=100))

print(type(t1))

print(vars(t1))

python

<class ‘type’>

{‘x’: ‘Python’, ‘y’: 100, ‘__module__’: ‘__main__’, ‘__doc__’: None}


The isinstance() function checks if a given object belongs to a specified class or data type.

python

isinstance(object, classinfo)

  • object: The object to be checked.
  • classinfo: The class or data type to compare against. It can be a single type or a tuple of types.
  • Returns True if the object is an instance of the specified class or data type.
  • Returns False otherwise.

python

age = isinstance(51, int)

print(“Age is an integer:”, age)

Output: Age is an integer: True

python

pi = isinstance(3.14, float)

print(“Pi is a float:”, pi)

Output: Pi is a float: True

python

message = isinstance(“Hello World”, str)

print(“Message is a string:”, message)

Output: Message is a string: True

python

my_tuple = isinstance((1, 2, 3), tuple)

print(“my_tuple is a tuple:”, my_tuple)

Output: my_tuple is a tuple: True

python

my_set = isinstance({1, 2, 3}, set)

print(“my_set is a set:”, my_set)

Output: my_set is a set: True

python

my_list = isinstance([1, 2, 3], list)

print(“my_list is a list:”, my_list)

Output: my_list is a list: True

python

my_dict = isinstance({“A”: “a”, “B”: “b”}, dict)

print(“my_dict is a dict:”, my_dict)

Output: my_dict is a dict: True

python

class MyClass:

    _message = “Hello World”

_class = MyClass()

print(“_class is an instance of MyClass():”, isinstance(_class, MyClass))

Output: _class is an instance of MyClass(): True


Featuretype()isinstance()
PurposeIdentifies the type of an object.Checks if an object is an instance of a class.
Return ValueReturns a class type (e.g., <class ‘str’>).Returns True or False.
Subclass CheckDoes not support subclass checking.Supports subclass checking.

python

class A:

    my_listA = [1, 2, 3]

class B(A):

    my_listB = [4, 5, 6]

print(type(A()) == A)        # Output: True

print(type(B()) == A)        # Output: False

print(isinstance(A(), A))    # Output: True

print(isinstance(B(), A))    # Output: True

Python time.sleep() Function

Download Python

Leave a Comment

Index