
Python type() and isinstance() Functions with Examples
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.
What is type() in Python?
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.
Python type() and isinstance() Functions Syntax
The type() function can be used in two ways:
- type(object) – Returns the type of the object.
- type(name, bases, dict) – Creates a new type object.
Python type() and isinstance() Functions Parameter
- 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.
👉Return Value
- When passing one parameter, type() returns the data type of the object.
- When passing three parameters, type() returns a new class object.
👉Examples of type() Usage
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))
👉Output:
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’>
👉Example: Using type() for Class Objects
python
class Test:
s = ‘testing’
t = Test()
print(type(t))
👉Python type() and isinstance() Functions Output:
javascript
<class ‘__main__.Test’>
👉Example: Using type() with Three Parameters
python
class MyClass:
x = ‘Hello World’
y = 50
t1 = type(‘NewClass’, (MyClass,), dict(x=’Python’, y=100))
print(type(t1))
print(vars(t1))
👉Output:
python
<class ‘type’>
{‘x’: ‘Python’, ‘y’: 100, ‘__module__’: ‘__main__’, ‘__doc__’: None}
👉What is isinstance() in Python?
The isinstance() function checks if a given object belongs to a specified class or data type.
👉Syntax for isinstance()
python
isinstance(object, classinfo)
👉Parameters for isinstance()
- 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.
👉Python type() and isinstance() Functions Return Value
- Returns True if the object is an instance of the specified class or data type.
- Returns False otherwise.
👉Examples of isinstance() Usage
1. Integer Check
python
age = isinstance(51, int)
print(“Age is an integer:”, age)
Output: Age is an integer: True
2. Float Check
python
pi = isinstance(3.14, float)
print(“Pi is a float:”, pi)
Output: Pi is a float: True
3. String Check
python
message = isinstance(“Hello World”, str)
print(“Message is a string:”, message)
Output: Message is a string: True
4. Tuple Check
python
my_tuple = isinstance((1, 2, 3), tuple)
print(“my_tuple is a tuple:”, my_tuple)
Output: my_tuple is a tuple: True
5. Set Check
python
my_set = isinstance({1, 2, 3}, set)
print(“my_set is a set:”, my_set)
Output: my_set is a set: True
6. List Check
python
my_list = isinstance([1, 2, 3], list)
print(“my_list is a list:”, my_list)
Output: my_list is a list: True
7. Dictionary Check
python
my_dict = isinstance({“A”: “a”, “B”: “b”}, dict)
print(“my_dict is a dict:”, my_dict)
Output: my_dict is a dict: True
8. Class Object Check
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
👉Key Differences Between type() and isinstance()
Feature | type() | isinstance() |
Purpose | Identifies the type of an object. | Checks if an object is an instance of a class. |
Return Value | Returns a class type (e.g., <class ‘str’>). | Returns True or False. |
Subclass Check | Does not support subclass checking. | Supports subclass checking. |
Example: type() vs isinstance() with Subclasses
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