29
loading...
This website collects cookies to deliver better user experience
type()
function takes in an argument and returns the type of the object. Apart from this method the isinstance()
function can check if an argument is an instance of a particular class or type. type()
is used to check the type of an object. This function exists for this exact purpose and is the most commonly used method. type()
method takes in an argument (object) and returns the class type assigned to the particular object. type(object)
language = "Python"
year = 1991
version = 3.9
print(type(language))
print(type(year))
print(type(version))
<class 'str'>
<class 'int'>
<class 'float'>
type()
to check the type of an object. Isinstance()
can also be used to check type. However, unlike the type()
function here we pass the classinfo along with the object the function returns a boolean value. isinstance(object, type)
language = "Python"
year = 1991
version = 3.9
print(isinstance(language, str))
print(isinstance(year, int))
print(isinstance(version, int))
True
True
False
isinstance()
method. type()
method is the most recommended method to check the type of an object. The isinstance()
method stands out as it can be used to check if an object is part of a class as well. type()
method to check the type of an object.