Get Variable Type Python

Introduction

Python has two functions type() and isinstance() that can be easily used to check variable' datatype.

Difference between type() and isinstance()

type() returns type of and object

isinstance() returns boolean value - if object is of specified type or not.

type()

Built-in type() function is the obvious solution to check the datatype.

It can be used in the following way.

print(type(variable))

Example with type()

There are fourteen datatypes in Python.

Let' start with three numeric types:

Create three variables of different numeric types and test how the function works:

var_int = 1380 var_float = 3.14 var_complex = 2.0-3.0j print(type(var_int)) print(type(var_float)) print(type(var_complex))

<class 'int'> <class 'float'> <class 'complex'>

Let's look at a few more examples

# Text Type: var_str = 'heihei.ru' # Boolean Type: var_bool = True # Sequence Types: var_list = ['heihei.ru','topbicycle.ru','eth1.ru'] var_tuple = ('aredel.com', 'aredel.com') var_range = range(0,9) print(type(var_str)) print(type(var_bool)) print(type(var_list)) print(type(var_tuple)) print(type(var_range))

<class 'str'> <class 'bool'> <class 'list'> <class 'tuple'> <class 'range'>

You can read the specification of the type() function on the website docs.python.org

The type command

There is also a useful type command that solves a different problem.

With the command type you can, for example, find out where Python is installed.

You can read more about this here

type python3

python3 is hashed (/usr/bin/python3)

type python

python3 is hashed (/usr/bin/python)

isinstance()

Besides type(), Python has the function isinstance(), with which you can check whether the variable belongs to some particular type.

Sometimes this is very convenient, and if needed - you can always, based on isinstance() write your own function.

Usage example

You can make an analogue of type() from isinstance()

Let's simplify the task by considering only five data types, create five variables of different types and check how the function works

var_int = 1380 var_str = 'heihei.ru' var_bool = True var_list = ['heihei.ru','topbicycle.ru','eth1.ru'] var_tuple = ('aredel.com', 'aredel.com') if (isinstance(var_int, int)): print(f"{var_int} is int") else: print(f"{var_int} is not int") if (isinstance(var_str, str)): print(f"{var_str} is str") else: print(f"{var_str} is not str") if (isinstance(var_bool, bool)): print(f"{var_bool} is bool") else: print(f"{var_bool} is not bool") if (isinstance(var_list, list)): print(f"{var_list} is list") else: print(f"{var_list} is not list") if (isinstance(var_tuple, tuple)): print(f"{var_tuple} is tuple") else: print(f"{var_tuple} is not tuple")

Result

1380 is int heihei.ru is str True is bool ['heihei.ru', 'topbicycle.ru', 'eth1.ru'] is list ('aredel.com', 'aredel.com') is tuple

Let's write our own function to determine the type typeof() based on isinstance

def typeof(your_var): if (isinstance(your_var, int)): return 'int' elif (isinstance(your_var, bool)): return 'bool' elif (isinstance(your_var, str)): return 'str' elif (isinstance(your_var, list)): return 'list' elif (isinstance(your_var, tuple)): return 'tuple' else: print("type is unknown")

Let's test our function

print(f"var_list is {typeof(var_list)}")

var_list is list

In other languages

Article author: Andrei Olegovich