In this article, we are going to learn all about the Python isinstance() method to check a specified object is of the specified type or not. isinstance method in python returns an only a Boolean value that means True or False.
In the previous tutorial, we have seen all about the Python input function to ask input from the users.
Headings of Contents
Python isinstance function
Python isinstance() function is a built-in function in Python that returns True if the specified object is of the specified type.
If the type parameter is a tuple, this function will return True if the objects are one of the types in the tuple.
Syntax
The syntax of isinstance function in Python is:-
isinstance(object, type)
Parameter
isinstance function accepts two-parameter.
- object:- Required, An object.
- type:- A type or class, A tuple of classes or types.
Return Value
The returned value of the python isinstance()
function is only Boolean value True or False.
Python isinstance() examples
Here we will take some examples to understand the python isinstance()
function.
Example 1:
print(isinstance(5, int))
print(isinstance('Programming Funda', str))
print(isinstance(12.21, float))
print(isinstance([1, 2, 3, 4], list))
print(isinstance((1, 2, 3, 4), tuple))
print(isinstance({1, 2, 3, 4, 5}, set))
Output
True
True
True
True
True
True
Example 2:
class Person:
name = 'Programming Funda'
def __init__(self):
print(f"My name is {self.name}.")
#create instance of the class Person.
p1 = Person()
print(isinstance(p1, Person))
Output
My name is Programming Funda.
True
Conclusion
In this article, you have learned all about the isinstance() function in Python to check a specified object is of the specified type or not. This function returns one boolean value that means True or False.
If you like this article, please share and keep visiting for further python built-in functions tutorials.
Python built-in functions
For more information:- Click Here
Thanks for your valuable time 👏👏👏