Python type Function

In this article we’ll go through the Python type function and it’s uses, complete with several examples.

The Python type Function has two uses. The first, if a single object is passed into it’s parameters, it will return the type of that object. The second, it can be used to create a new type object if three inputs are given.

Syntax

There are two different syntax’s for the type() function mentioned below. The first simply returns the type object. The second is used to create a new type object out of the parameters passed to it.

type(object)
type(name, bases, dicts)

Use 1 Examples:

Here are some example using the type function on a few basic data types. Since python is an object oriented language, you’ll see that classes are returned.

>>> type(5)
<class 'int'>

>>> type([5])
<class 'list'>

>>> type(5.0)
<class 'float'>

>>> type({5})
<class 'set'>

>>> type({ 1:"A" })
<class 'dict'>

This marks the end of the Python type Function. Any suggestions or contributions for CodersLegacy are more than welcome. Questions can be asked in the comment section below.

Here’s a link back to the main Python Built-in Functions page.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments