Python programming language provides different built-in types like an object, list, dictionary, integer, string, etc. It can be very handy to check types and compare different variable types. There are two popular methods called type()
and isinstance()
. These methods can be used to check specified object and variable types and make decisions according to the result.
Check Type with type() Method
The type method is one of the most popular methods to check the type of the specified object or variable or value. The object, variable, or value is provided to the type() object as the parameter. The syntax of the type() method is like below. The type() method is provided as built-in and there is no need to install or import any module.
type(OBJECT)
type(VARIABLE)
type(VALUE)
- OBJECT can be any object which type is returned.
- VARIABLE can be any variable which type is returned.
- VALUE can be any value which type is returned.
In the following example we check different objects, variables, and values type with the type() method.
a = 1
b = "String"
c = 4.5
d = [1,2,3]
e = {"1":"ismail","2":"ahmet"}
#Check type with Object and Variables
type(a)
#<class 'int'>
type(b)
#<class 'str'>
type(c)
#<class 'float'>
type(d)
#<class 'list'>
type(e)
#<class 'dict'>
#Check type with Values
type(1)
#<class 'int'>
type("String")
#<class 'str'>
type(4.5)
#<class 'float'>
type([1,2,3])
#<class 'list'>
type({"1":"ismail","2":"ahmet"})
#<class 'dict'>
Check Type with isinstance() Method
The isinstance() method provides more advanced usage than the type() method. The isinstance() method accepts 2 parameters. The first parameter is an object, variable, or value that is checked against the second parameter which is a type definition like str, integer, float, dict, list, etc. The isinstance() method syntax is like below.
isinstance(OBJECT,TYPE)
isinstance(VARIABLE,TYPE)
isinstance(VALUE,TYPE)
- OBJECT type checked against the TYPE.
- VARIABLE type checked against TYPE.
- VALUE type checked against TYPE.
- TYPE can be str, integer, float, dict, list etc.
Check String Type
a = "ismail"
isinstance(a,str)
#True
isinstance("ismail",str)
#True
Check Integer Type
a = 2
isinstance(a,integer)
#True
isinstance(3,str)
#True
Check Float Type
a = 1.5
isinstance(a,float)
#True
isinstance(1.5,str)
#True
Check Tuple Type
a = (1,2,3)
isinstance(a,tuple)
#True
isinstance((1,2,3),tuple)
#True
Check List Type
a = [1,2,3]
isinstance(a,list)
#True
isinstance([1,2,3],list)
#True
Check Dict Type
a = {"1":"ismail","2":"ahmet"}
isinstance(a,dict)
#True
isinstance({"1":"ismail","2":"ahmet"},dict)
#True
Check Multiple Types with isinstance() Method
The isinstance() method can be used to check multiple types for a single object, variable, or value. Multiple types are provided as a tuple and provided as the second parameter to the isinstance() method. The syntax of checking multiple types with instance() method is like below.
isinstance(OBJECT,TYPES)
isinstance(VARIABLE,TYPES)
isinstance(VALUE,TYPES)
- OBJECT checked against the multiple TYPES.
- VARIABLE checked against the multiple TYPES.
- VALUE checked against the multiple TYPES.
- TYPES is a tuple which contains single or more types to check aginst provided object, variable or value. As an example TYPES can be (str, integer,list).
In the following example, we check provided object, variable, and value against multiple types like integer, list, and str.
a = 1.5
b = "ismail"
c = [1,2,3]
isinstance(a,(int,list,str))
#False
isinstance(b,(int,list,str))
#True
isinstance(c,(int,list,str))
#True