How to check data type in python
The data type represents the nature of a variable; it determines what kind of data it can store and what operations can be carried out on it. There are five standard data types in Python programming language:
- Numeric
- Boolean
- Sets
- Dictionary
- Sequences type
- String
- Tuple
- Lists
It is important to know the data type of a variable to perform operations on it. As Python is a dynamically typed language, we need not to specify the data type while declaring a variable but still, to use variables in logics, we need to know the data type.
Ways to check Data Type in Python
We can find the data type of a variable in Python using two different methods:
- The type () method
- The isinstance () method
Generally, we use the type () method more because it is designed in the Python libraries especially for this, while isinstance () method has other kinds of applications too.
- The type () method
We can find the data type or the class type of the variable or the object passed as an argument in the type () function at the run time.
Syntax:
- Single argument: type (object)
It returns the data type of the specified object in the function.
- Three arguments: type (name, base, dictionary)
It returns a new type object or a new class at run time
Here, name refers to the name of the class to be created,
base is a tuple that contains base classes for the main class,
dictionary contains the body of the class to be created.
Programs:
- type () function with single argument
Program:
list1 = [1, 2, 3, 4, 5]
print ("The type of", list1, "is", type (list1))
string1 = "Hi!"
print ("The type of", string1, "is", type (string1))
tuple1 = (1, 2, 34)
print ("The type of", tuple1, "is", type (tuple1))
Output:
The type of [1, 2, 3, 4, 5] is <class 'list'>
The type of Hi! is <class 'str'>
The type of (1, 2, 34) is <class 'tuple'>
Explanation:
It simply specifies the type of the argument given in the function
- type () function with three arguments
Program:
object1 = type ('X', (object,), dict (a = 'yes', b = 6))
print (type (object1))
print (vars (object1))
class test:
a = 'yes'
b = 6
object2 = type ('Y', (test,), dict (a = 'yes', b = 6))
print (type (object2))
print (vars (object2))
Output:
<class 'type'>
{'a': 'yes', 'b': 6, '__module__': '__main__', '__dict__': <attribute '__dict__' of 'X' objects>, '__weakref__': <attribute '__weakref__' of 'X' objects>, '__doc__': None}
<class 'type'>
{'a': 'yes', 'b': 6, '__module__': '__main__', '__doc__': None}
Explanation:
First, we used the type function with a single base class that is the object class and named it ‘X’. We used the type () function with one argument ‘X’ to return the data type which gave “type” class. The vars () function in python is used to represent the __dict__ attribute of the class.
In the second case, we gave a created base class, the test class in which we specified the body of the class.
- The isinstance() method:
The isinstance method is much preferred because of its ability to check if the given object is an instance of the specified subclass. It takes two parameters, object and type of a class.
Syntax:
isinstance (object, class/ tuple of classes)
Return type: boolean
Mechanism:
The function returns “True” if the object which is the first argument belongs to the class as an instance specified as the second argument. If not, it returns False.
Program:
a = isinstance ("Hi", (int))
print ("Does 'Hi' belong to the integer class? ", a)
b = isinstance (5, (int, float, str, dict))
print ("Do 5 belong to any of int, float, str or dict classes?", b)
class myclass:
name = "hardin"
x = myclass ()
y = isinstance (x, myclass)
print ("Is x an object of myclass?: ",y)
Output:
Does 'Hi' belong to the integer class? False
Do 5 belong to any of int, float, str or dict classes? True
Is x an object of myclass?: True
Explanation:
First, we checked if the string “Hi” belongs to the integer class which does not; so we got False. Then, we checked if 5 which is an integer belongs to any of int, float, dict and string classes which it does, hence we got true. Then, we created a class “myclass” and created an object to the class x. Now, we check if x is an object to the class myclass, which it is, so we got “true”.
Type () or isinstance ()?
Mostly, people use the type () method more but it is preferred to go to the isinstance () method for many reasons:
- If we want to check if an object belongs to any class among a few classes, we can simply specify all the classes and get a true of false in the case of the isinstance method. But, in the type (0 method, we get true, only when we use the exact same type object on both sides.
- Type () method does not support the inheritance which is a core of the object oriented programming
If we just need the data type to be printed, we can use the type method but if we need to check, isinstance () method is preferred more.