Posted in

Python type(): What are type() and isinstance() functions in Python?

Python type() and isinstance() function
Python type() and isinstance() function

Python type()

Python type() with a single argument

Code:
data1 = 'This is Python programming.'
data2 = 104104
data3 = ['Python', 'Java', 2]
print(type(data1))
print(type(data2))
print(type(data3))

Output:
<class 'str'>
<class 'int'>
<class 'list'>
Code:
print(type('This is Python.'))
print(type(100))

Output:
<class 'str'>
<class 'int'>
Code:
print(type('This is Python.', 100))

Output:
TypeError: type() takes 1 or 3 arguments
Code:
print(type('Python') is str)
print(type('Python') is int)

Output:
True
False
Code:
print(type('Python') is string)

Output:
NameError: name 'string' is not defined.
Code:
x = 'This'
y = 'That'
if type(x) is type(y):
    print('Yes, x and y has same data type.')
    print('x and y has:', type(x))
else:
    print('No, x and y has different data type.')
    print('x has:', type(x))
    print('y has:', type(y))

Output:
Yes, x and y has same data type.
x and y has: <class 'str'>
Code:
x = 'This'
y = 100
if type(x) is type(y):
    print('Yes, x and y has same data type.')
    print('x and y has:', type(x))
else:
    print('No, x and y has different data type.')
    print('x has:', type(x))
    print('y has:', type(y))

Output:
No, x and y has different data type.
x has: <class 'str'>
y has: <class 'int'>

type() with expression or equation

Code:
print(type(11>2))

Output:
<class 'bool'>

type() with class object

Code:
class Myclass:
    print("We have created a new class")
Working = Myclass()
print(type(Working))

Output:
We have created a new class
<class '__main__.Myclass'>

type() with a statement

Code:
first_list = ['Python', 'Java']
checking = 'Python' in first_list
print(type(checking))

Output:
<class 'bool'>
Code:
a = 1
b = 2
c = a + b
print(type(c))

Output:
<class 'int'>
Code:
def first_function():
    print('abc')
a = first_function.__doc__
print(type(a))

Output:
<class 'NoneType'>

Python type() with three arguments

Code:
new = type('FirstClass', (), dict(a='Python'))
print(type(new))

Output:
<class 'type'>
Code:
new = type('FirstClass', (object,), {'a': 'Python'})
print(type(new))

Output:
<class 'type'>

Python isinstance() Function

Code:
checking = isinstance(4, int)
print(checking)

Output:
True
Code:
checking = isinstance(4, str)
print(checking)

Output:
False
Code:
checking = isinstance(4, integer)
print(checking)

Output:
NameError: name 'integer' is not defined
Code:
checking = isinstance(4, (int,str,float))
print(checking)

Output:
True
Code:
checking = isinstance(4, (list,str,float))
print(checking)

Output:
False
Code:
checking = isinstance(['Python', 'Java'], list)
print(checking)

Output:
True

So here we are checking list data type. We can provide arguments as variables too. For example:

Code:
a = (1,2,3)
b = tuple
print(isinstance(a,b))

Output:
True
Code:
checking = isinstance('python', 100, str, int)
print(checking)

Output:
TypeError: isinstance expected 2 arguments, got 4
Code:
class MyClass:
    value = "Python"
first_object = MyClass()
print(isinstance(first_object, MyClass))

Output:
True

Difference Between type() and isinstance() Function

Conclusion

2 thoughts on “Python type(): What are type() and isinstance() functions in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *