Python type() function
Published February 17, 2022Python 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] |
output
<class 'list'> |
Article Contributed By :
|
|
|
|
320 Views |