Methods and Functions

Python Type()

In order to debug what type of data is stored in the variables, the Python programming language provides us with a function. This function lets us know which data type is used. The name of this function is Python type(). Python type() function is a built-in function in python that allows the programmer to evaluate the type of data used in the program for every variable.

Definition

  • Python type() is a built-in function that returns the type of the objects/data elements stored in any data type or returns a new type object depending on the arguments passed to the function.
  • The Python type() function prints what type of data structures are used to store the data elements in a program.

Python Type()

In Python, we do not explicitly specify the data type of the variable for storing data elements. Hence, if we want to find out what type of data is stored in a variable we use Python’s built-in type() function. Python type() is a built-in function that is used to print the type of function parameters based on the arguments passed to it. There are 2 different forms of type() functions i.e. there are 2 different methods to pass the arguments into the type() function.

The syntax is as follows –

                    

type(object)

type(name, bases, dict)

where,

name = name of the class, which becomes ___name___ attribute

bases = tuple from which current class is derived, becomes ___bases___ attribute

dict = a dictionary which specifies the namespaces for the class, later becomes ___dict___ attribute

The first method accepts a single parameter in the function. While the second method accepts 3 parameters in it.

If you pass a single argument to the type(object) function, it will return the type of the object. When you pass 3 arguments to the type(name, bases, dict) function, it creates a class dynamically and returns a new type object.

Python Type() with Single Parameter

We have already seen the syntax for passing a single argument to the type() function. Let us look at a few examples to understand this type() form.

Example 1

                    

a = 10
b = 10.5
c = True
d = 1 + 5j

print(type(a))
print(type(b))
print(type(c))
print(type(d))

Output

                    

<class 'int'>
<class 'float'>
<class 'bool'>
<class 'complex'>

Example 2

                    

my_str = 'Hello World!'
my_list = [1, 2, 3, 4, 5]
my_tuple = (10, 'Hello', 45, 'Hi')

print(type(my_str))
print(type(my_list))
print(type(my_tuple))

Output

                    

<class 'str'>
<class 'list'>
<class 'tuple'>

Example 3

                    

my_tuple = (10, 'Hello', 45, 'Hi')
my_dict = {1: 'One', 2: 'Two', 3: 'Three'}

if type(my_tuple) is not type(my_dict):
      print("Both variables have different object types.")
else:
      print("Same Object type")

Output

                    

Both variables have different object types.

Python type() with 3 Parameters

This form of type() function returns a new type object. The 3 arguments passed to the function are name, bases, and dict. Let us understand by looking at an example.

Example

                    

a = type('Test', (object, ), dict(x = 'Hello', y = 10, z = 'World'))

print(type(a))
print(vars(a))

Output

                    

<class 'type'>

{'x': 'Hello', 'y': 10, 'z': 'World', '__module__': '__main__', 
'__dict__': <attribute '__dict__' of 'Test' objects>, 
'__weakref__': <attribute '__weakref__' of 'Test' objects>, '__doc__': None}

Understanding the above code –

  • We created a type object by passing 3 arguments to the Python type() function. We declared the name of the object as Test, its base as an object, and initialized a dictionary having 3 keys and values.
  • The first print statement print(type(a)) will print the class object as type. In the second print statement, we have used the ‘vars()’ function. The vars() function is used to return the __dict__ attribute of an object. Dictionary is used to store the object’s writable attributes in the __dict__ attribute.
  • In the output above, __weakref__ attribute/module is used to create weak references to the object. In this case __weakref__ of Test (object_name) for object (base). Weak references are used to hold large objects.

Applications of type() function

  1. In Python, we do not declare the data type for the variable. We directly define the variable and store the values in it. Using this Python built-in function, we are able to identify which variable contains what type of data. This function is very useful for debugging purposes.
  2. Another function of type() is that we can dynamically create classes along with their attributes during the run-time of the program. This is possible by passing 3 arguments to the type() function.
  3. Python type() function is also useful to create tables in the database using SQL.

FAQs

Q1. What will be the output of the following program?

                    

a = 5
b = 10.5

c = a + b
print(type(c))

  1. <class ‘int’>
  2. <class ‘float’>
  3. <class ‘double’>
  4. Syntax Error

Answer. Option B. The value of variable ‘c’ after addition will be 15.5 which is a float value.

Q2. What is the correct syntax for creating a type object in Python?

  1. type(object)
  2. type(object, base, tuple)
  3. type(object_name, base, dict)
  4. type(base, object_name, dict)

Answer. Option C

Q3. What is the use of type() function in Python program?

Answer. Python type() is a built-in function that is used to return the type of data stored in the objects or variables in the program. For example, if a variable contains a value of 45.5 then the type of that variable is float. Another example can be, if variable ‘subj’ stores the value ‘Python’, then the type of ‘subj’ is a string.  It is also used for creating dynamic classes along with their attribute values using the dictionary data type.

Q4. What will be the output of the following code?

  1. <class ‘bool’>
  2. <class ‘str’>
  3. <class ‘list’>
  4. Syntax Error

Answer. Option A. The value of a is True and not a string ‘True’. Hence the data type of variable is Boolean.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.