Register Login

How to Check Data Type in Python

Data type plays a significant role in almost all programming languages. This article contains descriptive information of the type() and isinstance() functions in Python.

Extracting the type of variable Python is holding is important to debug and testing purposes. In this article, you will have a clear understanding of the 2 approaches to fetch the type of a variable and how these two techniques are different.

You can refer to the examples below to gain a better understanding.

Data Types in Python

The data types used in Python are:

  1. Number: This data type consists of integers, complex numbers, etc.
  2. String: It consists of Unicode characters- a global coding standard.
  3. Boolean: It represents True and False.
  4. List: Lists are mutable. We can change the values inside the lists even after creating them.
  5. Tuple: Tuple consists of elements of different data types in an ordered form. We cannot change the values inside the tuple after creating them.
  6. Set: A set consists of unique items in an unordered form.
  7. Dictionary: A dictionary contains key-value pairs in an unordered form.

The isinstance() function in Python:

To test any object in Python, we use the 'isinstance()' function. This function takes subclasses into account. It means that the isinstance() function will return TRUE only when a stated object is of the desired type. Again, we see FALSE in other cases. We use the 'type()' function in Python for obtaining the variable type.

That is what is necessary for this article. In Python, each value contains a data type. Python programming treats everything as an object. The isinstance() helps programmers to check the type of variable. It accepts two parameters.

The first parameter determines the variable which we want to check while the second parameter determines the standard Python type (as mentioned in the list above).

Program:

x = isinstance("Hello", str)
print(x)

Output:

Check Data Type using type():

In general, we use the type() function to check the data type of any variable used in Python. The type() function can return the data type with a single argument. However, the data types used in Python are a bit different from those used in C or C++.

Syntax:

type(object)
type(name, bases, dict)

Example:

# the type() function
print(type([]) is list)
print(type([]) is not list)
print(type(()) is tuple)
print(type({}) is dict)
print(type({}) is not list)

Output:

Another example where we can use the type() for using it as condition:

class Dicto:
    DictNumber = {1:'Karlos', 2:'Ray', 3:'Dee', 4:'Su'}
      
class Listo:
    ListNumber = [6, 5, 4, 3, 2]

dp = Dicto()
lp = Listo()

if type(dp) is not type(lp):
    print("The class object types are different.")
else:
    print("The class object types are same.")

Difference Between type() Function and isinstance() Function

isinstance() in an in-built function in Python. It returns true or false. On the other hand,  the type() function returns the type of an object. In simple words, you use type() to check the exact type of object. You use an isinstance to confirm your suspicions about the type of object. When compared, isinstance() is better than the type() function in Python.

isinstance() type()
It returns a Boolean value It returns a String value
It takes two arguments It takes one argument
It checks for multiple classes It checks for only one object

Conclusion

We use the Python isinstance() function to check if a specific value holds a particular data type whereas type() provides us with the exact data type of that variable. Although both works faster, the type() is slightly efficient compared to the other because it takes only one parameter.

Also, for debugging, the isinstance() function is not efficient. But, if a programmer wants to use the answer is Yes/no format for debugging purpose or testing a particular section of the code for data type, the isinstance() is the best alternative. But we can also use type() like a Boolean operation or result also.