Table of contents
1.
Introduction
2.
Python type() Function Syntax
2.1.
Syntax
3.
Parameters
4.
Return Value
5.
How type() Function Works in Python?
5.1.
Example
6.
Examples of the type() Function in Python
6.1.
Checking Types of Variables
7.
Using type() with Conditional Statements
7.1.
Example
8.
Python type() with 3 Parameters
8.1.
Example
9.
Applications of Python type() Function
10.
Real-Life Usage of the type() Function  
10.1.
1. Debugging & Understanding Data  
10.2.
2. Handling Different Data Types in Functions  
10.3.
3. Validating User Input  
10.4.
4. Working with Custom Objects  
11.
Frequently Asked Questions
11.1.
What does the type() function return in Python?
11.2.
How is type() different from isinstance()?
11.3.
Can we create classes dynamically using type()?
12.
Conclusion
Last Updated: Mar 4, 2025
Medium

type() Function in Python

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The type() function in Python is used to determine the type of an object. It helps in identifying data types such as integers, strings, lists, dictionaries, and custom objects. This function is useful for debugging and dynamic type checking. It can also be used with three arguments to create a new class. 

In this article, we will learn the type() function, its syntax, and various use cases with examples.

Python type() Function Syntax

Syntax

type(object)
 type(name, bases, dict)


The type() function can be used in two ways:
 

  1. With a single parameter to check the type of an object.
     
  2. With three parameters to dynamically create a new class.

Parameters

The type() function accepts the following parameters:

  1. object (optional): The variable or value whose type needs to be checked.
     
  2. name (required when using three arguments): The name of the new class being created.
     
  3. bases (required when using three arguments): A tuple that specifies the base classes for the new class.
     
  4. dict (required when using three arguments): A dictionary containing the attributes and methods of the new class.

Return Value

The type() function returns:

  1. When called with one argument, it returns the type of the given object.
     
  2. When called with three arguments, it returns a new type object (a new class).

How type() Function Works in Python?

The type() function is primarily used to check the data type of variables. It helps programmers write dynamic and error-free code by verifying variable types.

Example

x = 10
y = "Hello"
z = [1, 2, 3]

print(type(x))  
print(type(y))  
print(type(z))  
You can also try this code with Online Python Compiler
Run Code

 

Output

<class 'int'>
<class 'str'>
<class 'list'>

 

This example shows how type() determines the data type of different variables.

Examples of the type() Function in Python

Checking Types of Variables

num = 5.5
boolean_val = True
dictionary = {"name": "Alice", "age": 25}

print(type(num))        
print(type(boolean_val)) 
print(type(dictionary))
You can also try this code with Online Python Compiler
Run Code

 

Output

<class 'float'>
<class 'bool'>
<class 'dict'>


The type() function helps in identifying the correct type of data stored in variables.

Using type() with Conditional Statements

The type() function can be useful in decision-making inside a program.

Example

def check_data_type(value):
    if type(value) == int:
        print("The value is an integer.")
    elif type(value) == str:
        print("The value is a string.")
    elif type(value) == list:
        print("The value is a list.")
    else:
        print("Unknown data type.")

check_data_type(10)      
check_data_type("Hello") 
check_data_type([1, 2])  
You can also try this code with Online Python Compiler
Run Code

 

Output

The value is an integer.
 The value is a string.
The value is a list.

 

This example demonstrates how type() can be used in conditional statements to handle different types of data efficiently.

Python type() with 3 Parameters

The type() function can also be used to dynamically create classes by passing three parameters.

Example

# Creating a new class dynamically
MyClass = type('MyClass', (object,), {'x': 5})

obj = MyClass()
print(obj.x)  
You can also try this code with Online Python Compiler
Run Code

 

Output: 

5

 

Here, we create a class named MyClass dynamically using type().

Applications of Python type() Function

The type() function is widely used in Python programming for:

  1. Debugging: Helps in checking variable types to avoid runtime errors.
     
  2. Dynamic Class Creation: Used in frameworks where classes need to be created at runtime.
     
  3. Type Validation: Ensures that functions receive the correct data type as input.

Real-Life Usage of the type() Function  

The `type()` function is not just a theoretical concept; it has practical uses in everyday programming. Let’s explore some real-life scenarios where `type()` can be helpful.  

1. Debugging & Understanding Data  

When working on a project, you might encounter situations where you’re unsure about the data type of a variable. Using `type()`, you can quickly check the data type & debug your code.  

For example:  

x = 42  
print(type(x))  

y = 3.14  
print(type(y))

z = "Hello, Python!"  
print(type(z))   
You can also try this code with Online Python Compiler
Run Code

 

Output

<class 'int'>  
<class 'float'>  
<class 'str'> 


In this example, `type()` helps you confirm whether `x` is an integer, `y` is a float, & `z` is a string.  

2. Handling Different Data Types in Functions  

Sometimes, you might write a function that needs to handle different types of inputs. The `type()` function can help you check the input type & perform actions accordingly.  

For example:  

def process_data(data):  
    if type(data) == int:  
        print("Processing integer:", data  2)  
    elif type(data) == str:  
        print("Processing string:", data.upper())  
    else:  
        print("Unsupported data type")  


process_data(10)           
process_data("python")   
process_data([1, 2, 3])    
You can also try this code with Online Python Compiler
Run Code

 

Output

Processing integer: 20 
Processing string: PYTHON  
Unsupported data type 


Here, the `process_data()` function uses `type()` to handle integers & strings differently.  

3. Validating User Input  

When building applications, you often need to validate user input. The `type()` function can help ensure the input is of the expected type.  

For example:  

user_input = input("Enter a number: ")  
if type(eval(user_input)) == int:  
    print("Valid integer input!")  
else:  
    print("Invalid input. Please enter an integer.")  


In this example, `type()` is used to check if the user’s input is an integer.  

4. Working with Custom Objects  

The `type()` function is also useful when working with custom classes & objects. It helps you identify the class of an object.  

For example:  

class Dog:  
    def __init__(self, name):  
        self.name = name  
my_dog = Dog("Buddy")  
print(type(my_dog))   
You can also try this code with Online Python Compiler
Run Code

 

Output: 

<class '__main__.Dog'>  


Here, `type()` confirms that `my_dog` is an instance of the `Dog` class.  

Frequently Asked Questions

What does the type() function return in Python?

The type() function returns the type of the given object when called with one argument. When called with three arguments, it returns a new type (class) object.

How is type() different from isinstance()?

The type() function checks for the exact type of an object, whereas isinstance() checks if an object is an instance of a specific class or its subclass.

Can we create classes dynamically using type()?

Yes, using type() with three parameters allows the creation of new classes dynamically.

Conclusion

In this article, we discussed the type() function in Python, which is used to determine the data type of a given object. It helps in dynamic type checking and can also be used for creating new types dynamically. Understanding type() is essential for writing robust and flexible Python programs that handle different data types efficiently.

Live masterclass