![type() function in Python with Examples type and isinstance in Python with Examples](https://www.h2kinfosys.com/blog/wp-content/uploads/2020/12/type-and-isinstance-in-Python-with-Examples.jpg)
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)) |
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)) |
Facebook Comments