Member-only story
Hi everyone, welcome back. Today, we will be going over the type() function in Python and see how it works. The type() function is a built in function and its purpose is to return the data type of the given object. Let’s get into it.
Type Function
Let’s see how the type function works:
object = 3
print(type(object))Output:
<class 'int'>
We created an int object and set it to 3. Then we have a print statement which will output the type of the object. As we can see in the output, we can see that we successfully retrieved the int type. Let’s see the type() function with other data types.
Example with other types
object1 = False
object2 = 1.1
object3 = "string"
print(type(object1))
print(type(object2))
print(type(object3))Output:
<class 'bool'>
<class 'float'>
<class 'str'>
The type() function will also return other data types, such as a boolean, a float, and a string.
Example with collections of data
object1 = (1, 2, 3)
object2 = [1, 2, 3]
object3 = {1, 2, 3}
print(type(object1))
print(type(object2))
print(type(object3))Output:
<class 'tuple'>
<class 'list'>
<class 'set'>
The type() function will also return a tuple, list, and a set.
Conclusion
This is it for the type() function examples. It’s a straight forward function that returns the data type of a given object. I hope this helps. Thanks for reading.