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.
my_var = 'Hello World'
print(type(my_var))
<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.
my_var = '123456789'
print(type(my_var))
<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.
my_var = 123456789
print(type(my_var))
<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.
my_var = 1.23456789
print(type(my_var))
<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.
my_var = 1 + 1j
print(type(my_var))
<class 'complex'>
Sequence Types
Let’s store some different sequence types in our my_var variable and inspect the result with the type() function.
my_var = ['my', 'favorite', 'type']
print(type(my_var))
<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.
my_var = (1, 2, 3, 'fee', 'fi', ['fo'], {'fum'})
print(type(my_var))
<class 'tuple'>
Now we can look at a dictionary sequence type.
my_var = {'key': 'value'}
print(type(my_var))
<class 'dict'>
In this next snippet, we see the Boolean type.
my_var = True
print(type(my_var))
<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.
import datetime
my_var = datetime
print(type(my_var))
<class 'module'>
import pandas as pd
my_var = pd.Series(['some', 'cool', 'stuff'])
print(type(my_var))
<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.