Hit enter after type your search item

What is type() function with single argument?

In some scenarios, you may require knowing the data type of an object or variables as doing programming in Python. The Python type() function is one of the available ways for doing this.

Type() list example Tuple type string, float, int example

By passing the object to the type() function, the single required parameter, it returns the type of the object like a list, integer, float, tuple etc.

Syntax of type Python function

The syntax of using the type() function for checking the type of object or variable is:

class type(object)

The other use of type() function takes three parameters. That is:

class type(name, bases, dict)

It returns a new type object. (See the last part of this tutorial).

Note: All the examples below show how to use the type function with the single parameter.

An example of type function

In this example, a list is created with few items. The type() function is used to return its type:

See online demo and code

type function

The returned value is <class ‘list’>.

A demo of tuple with type function

For this example, the tuple object is passed to the type function, see what it returns:

See online demo and code

Python type

A dictionary object example

See this example where a dictionary object is created and its type is checked by using the Python type() function.

See online demo and code

type dict

The returned value is <class ‘dict’>.

Knowing the type of integer, float and string variables

As such, the int, float and string variables are also objects. You may pass these into the type() function to know their data type. This may be useful for scenarios where you need to read the data, convert to the appropriate type and perform some action.

In the following example, the types of a string, int, and float objects are displayed by using the type() function:

See online demo and code

type int float string

What is recommended way of testing the type?

The Python official website recommends using the isinstance() function for testing the type of the objects.

See an example of using the ininstance() function or go to its tutorial by clicking here.

See online demo and code


isinstance type

The returned value is true, that means it is a list.

The type function with three parameters

The other syntax used for the type() function takes three parameters. That is:

class type(name, bases, dict)

For more details on this, visit the official website section here.

This div height required for enabling the sticky sidebar