Python type() function

Published February 17, 2022

Python does not allow you to declare data types for variables; instead, you just initialise the variable and store its values. This makes determining the type of variable holding a certain piece of data difficult. We can determine which variable has the specific data type by using the Python type() built-in function. For example, if the variable holds the value 23.5, the variable's type is float.


This method returns the type of variable holding the data, which is highly useful for programmers, particularly during debugging operations.


Let's have a look at the type() function's syntax.


Syntax
The type() function is written as follows:

Type(object)

for the single parameter and  type(name, bases, dictionary) for the 3 parameters.


Where:
name:  is the name of the class
Bases: the base class
dictionary: a dictionary with namespace

Example
In the example below, we'll find the type of the variable using type() function
 

x = [5,6,7,3,7]
find_type = type(x)
print(find_type)

output

<class 'list'>

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

320 Views