How to get data type of variable in Python

Data Types in Python?

Python has several built-in data types. The most commonly used data types are:

Numeric Types: int for whole number, float for number with decimal, and complex for number with a real part and an imaginary part

Sequence Types: str for text, list for an ordered, mutable sequence of elements, and tuple for an ordered, immutable sequence of elements.

Set Types: set is used for unordered collection of unique elements.

Mapping Type: dict for collection of key-value pairs

Boolean Type: bool for 'True' or 'False'

None Type: None for the absence of a value or a null value

Getting data type of variable in Python

In Python, you can get the data type of a variable using the type() built-in function.

In the following example, "Welcome to Python World" has been assigned to the variable 'message'. type() function has been used to get the data type of the variable.

Please take a closer look at the following example:

Example of Using type() function


Output of the above example

The data type of message variable is <class 'str'>

The output displays data type of 'message' variable as 'str' class.

More examples of getting data type of variable


Output of the above examples

The data type of number variable is <class 'int' >
The data type of num_float variable is <class 'float' >
The data type of var_list variable is <class 'list' >
The data type of var_tuple variable is <class 'tuple' >
The data type of var_dictionary variable is <class 'dict' >

Live Code Playground

In the following Python code editor, you can practice the above code by declaring more variable and using type() function to data type of Python variables. Click on the 'Execute Code' button to execute the code; the executed output will be displayed in the following Python Code Output frame. Practice until you become comfortable and proficient with your code.

Python logo Python Online Editor
Python Code Output