Using the isinstance() function in Python 3

The isinstance() function in Python is used to check whether an object is an instance of a particular type.
Syntax:
isinstance(input_object, type)
Where:
- input_object: The object you want to check type
- type: A type, or a tuple of types
The function returns True if the type of the provided object matches the given type, otherwise False.
Example 1
a = 12
b = [1, 2, 3]
c = "KindaCode.com"
print(isinstance(a, int))
print(isinstance(b, list))
print(isinstance(c, bool))
Output:
True
True
False
Example 2
In this example, we will pass a tuple of types to the isinstance function to check if an object’s type is among them:
t = 1.23
print(isinstance(t, (int, float, bool, str)))
Output:
true
Further reading
- How to Install Python Libraries in Google Colab
- Python zip() function examples
- Python reduce() function examples
- Examples of using map() function in Python 3
- Extract all links from a webpage using Python and Beautiful Soup 4
You can also check out our Python category page for the latest tutorials and examples.
Subscribe
0 Comments