Python type() Function

Python type() Function

The type() function is a great tool to have when working with programs in Python. Everything in Python is an object, but there are many different types of objects available. There are string objects, function objects, integer objects, float objects, and boolean objects. It is useful to understand the type of an object when writing a Python script or during debugging. The function type() takes in an object as input and returns the data type of a given object. We’ll look at all kinds of examples in this tutorial to get a sense of how to use the type() function in Python.


Hello Types

Printing ‘Hello World’ is pretty much the first thing you do when learning any programming language. Let’s inspect this using the type() function.

<class 'str'>

We’ll use the same my_var variable for all of the examples in this tutorial to show that any given variable can store any kind of type. Of course, inspecting ‘Hello World’ with the type() function shows us that it is of type ‘str’. Let’s see a different example.

<class 'str'>

The variable once again is holding a type of ‘str’. Even though we see numbers, it is a string of numbers, not actual number types.


type() and numbers

Now let’s see some number types in Python by way of the type() function. In this example, we’ll simply remove the enclosing ' characters around those numbers. Watch how the result changes now.

<class 'int'>

Now we can see that my_var is holding an integer type which is used to represent whole numbers. Let’s make a small change to the code.

<class 'float'>

We can see that the number now has a decimal point in it. When using the type() function to inspect a number with a decimal point, we find that it is of type float. Numbers can also be a complex type. A Complex Number means two numbers added together (a Real and an Imaginary Number). Here is an example of that.

<class 'complex'>

Sequence Types

Let’s store some different sequence types in our my_var variable and inspect the result with the type() function.

<class 'list'>

As you can see, the my_var variable now holds an object of type list, one of my favorite types to work with thanks to how useful and flexible it is. Let’s look at this interesting example of the tuple data type.

<class 'tuple'>

Now we can look at a dictionary sequence type.

<class 'dict'>

In this next snippet, we see the Boolean type.

<class 'bool'>

Custom Data Types

The section above showed some of the built-in types in Python. When you’re working with different libraries and code, you might need to see what type you are working with. Consider this code here.

<class 'module'>

<class 'pandas.core.series.Series'>

Python type() Function Summary

Sometimes you might want to know the variable type in a given program. Maybe the type isn’t obvious from the code or you’ve received the information from a source whose code isn’t accessible. Whenever you want to see the type of a variable, you can use the type() function. As we saw from the many examples in this article, there are many different types to work with in Python.