How to check data type in Python

Data types are essential determine the kind of values that can be stored in a variable and the operations that can be performed on them .

Python has several built-in data types such as:

integer(int),floating- point number(float),strings(str),lists(list),tuples(tuple),dictionaries(dict)

Method 1: Using the type() Function

The simplest way to check the data type of an object in Python is to use the built-in type() function. This function returns the data type of the given object.

Method 2:Using the isinstance() Function

This function checks if a given object is an instance of the specified data type(s) and True or False.

Using the type() function

y = 5.14
print(type(y)) 

z = "Hello, world!"
print(type(z))  
a = [1, 2, 6]
print(type(a)

 

Output:

<class 'float'>
<class 'str'>
<class 'list'>

Using the ‘isinstance()’ for type checking:

b = {"name": "Tuppu", "age": 30}
print(isinstance(b, dict))  

c = (1, 5, 3)
print(isinstance(c, tuple))

Output:

True
True

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top