type and isinstance in Python with Examples

type() function in Python with Examples

Table of Contents

As we know, Python does not allow us to declare variables with the datatype mentioned. Python auto allocate the data type to the variable with respect to the data that we put inside the variable. There is a built-in function called type that returns the datatype of the variable.

Syntax of type function:

Parameters: type(object)

Object: This is a mandatory parameter. If this is the only parameter passed to type(), it will return you the parameter type.

my_list = [“A”, “B”, “C”, “D”]
my_tuple = (“A”, “B”, “C”, “D”)
my_dict = {“A”:”a”, “B”:”b”, “C”:”c”, “D”:”d”}
my_set = {‘A’, ‘B’, ‘C’, ‘D’}

str_list = “Hello World”
number = 50
pi_value = 3.14
complex_num = 3j+10

print(“The type is : “,type(my_list))
print(“The type is : “,type(my_tuple))
print(“The type is : “,type(my_dict))
print(“The type is : “,type(my_set))
print(“The type is : “,type(str_list))
print(“The type is : “,type(number))
print(“The type is : “,type(pi_value))
print(“The type is : “,type(complex_num))
type() function in Python with Examples

Example: Using type() for class object.

When we call the type function with input as an object of a class, it returns the class type along with the name of the class.

class car:
color = ‘blue’

a = car()
print(type(a))
type() function in Python with Examples

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class