Instance and Type both are used to check the type of object. Instance can check the type of subclass too where as type can't.
!python --version
Check if type is Integer
x = 1
print(type(x))
Check if type is Float
x = 1.5
print(type(x))
Check if type is String
x = 'john'
print(type(x))
x = 'j'
print(type(x))
Check if type is Class
class demo():
pass
print(type(demo()))
type(demo())==demo
isinstance can be used to check the type of object.
x = 1
isinstance(x,int)
isinstance(x,float)
x = 1.2
isinstance(x,float)
isinstance can check the type of subclass too.
class computer():
pass
class laptop(computer):
pass
isinstance(computer(),computer)
isinstance(laptop(),computer)
Note, type can not check the type of subclass.
type(laptop())==computer
Related Notebooks
- How to do SQL Select and Where Using Python Pandas
- Summarising Aggregating and Grouping data in Python Pandas
- Merge and Join DataFrames with Pandas in Python
- How to Convert Python Pandas DataFrame into a List
- How to Plot a Histogram in Python
- How to Generate Random Numbers in Python
- Python Pandas String To Integer And Integer To String DataFrame
- How to Upgrade Python PIP
- Most Frequently Asked Questions Python Pandas Part1