Python type() Function: Identify Data Types | Syntax, Examples and Use Cases

Introduction: Python type() Function

When working with Python, you often need to identify the type of data stored in a variable or object. Knowing whether a value is a number, string, list, or another data type helps you use it correctly in your program.

Without knowing the data type, it becomes easier to make programming mistakes, such as performing unsupported operations or passing incorrect values to functions.

The Python type() Function provides a simple and reliable way to identify an object’s data type.

What it is: The type() function is a built-in Python function that returns the type (class) of an object. It is commonly used to identify the data type of variables, values, and objects during program development and debugging.

Take a look at a quick example to see how the function behaves.

Then review its real-world use cases to discover where it is commonly applied.

Now let’s understand its syntax, parameters, and return value before exploring practical examples.

💡 Tip: After learning type(), continue building your Python skills with the complete Built-in Functions Learning Guide, covering the most commonly used built-in functions.

Syntax, Parameters, Return Value and Examples: Python type() Function

The following section explains the syntax, parameters, return value, and a quick example of the Python type() Function.

Syntax

The type() function has two forms:

1. Finding the type of an object

type(object)

2. Creating a new type (advanced usage)

type(name, bases, dict)

The second form is used for dynamically creating classes and is generally considered an advanced feature. In most Python programs, the first form is used to determine the type of an object.

Parameters

Parameter Description
object The object whose type needs to be identified.
name The name of the new class (used only in the three-argument form).
bases A tuple containing the base classes for the new class.
dict A dictionary containing the attributes and methods of the new class.

Return Value

Return Value Description
type Returns the type (class) of the specified object.

Quick Example

The following example determines the data type of a variable using the type() function.

age = 25

print(type(age))


# Output:
<class 'int'>

The type() function examines the object and returns its data type. In this example, the variable age stores an integer, so the function returns <class 'int'>.

How the Python type() function works

  • The type() function accepts an object as its argument.
  • It examines the object and identifies its data type.
  • The function returns the corresponding Python class.
  • It works with built-in data types as well as user-defined objects.
  • The returned type can be displayed, compared, or used in program logic.
  • The three-argument form of type() is used to create classes dynamically.

Examples: Python type() Function

The following examples show how the Python type() Function works in different programming scenarios.

Example 1: Finding the Type of an Integer

number = 25

print(type(number))


# Output:
<class 'int'>

Explanation: The type() function identifies that number stores an integer and returns <class 'int'>.

Example 2: Finding the Type of a String

name = "Alice"

print(type(name))


# Output:
<class 'str'>

Explanation: Since the variable stores text, the type() function returns the string class.

Example 3: Finding the Type of a List

fruits = ["apple", "banana", "mango"]

print(type(fruits))


# Output:
<class 'list'>

Explanation: The function identifies that the object is a Python list and returns <class 'list'>.

Example 4: Finding the Type of a Dictionary

student = {
    "name": "Alice",
    "age": 20
}

print(type(student))


# Output:
<class 'dict'>

Explanation: The type() function recognizes the object as a dictionary and returns its class.

Example 5: Comparing Data Types

value = 3.14

if type(value) == float:
    print("This is a float.")


# Output:
This is a float.

Explanation: The returned type is compared with float to determine the kind of data stored in the variable.

Example 6: Finding the Type of User Input

value = input("Enter something: ")

print(type(value))


# Sample Output:
Enter something: 100
<class 'str'>

Explanation: Even if the user enters a number, the input() function returns it as a string, which is confirmed using type().

Use Cases: When to use the type() Function

Below are some common situations where the Python type() Function becomes useful:

  • Checking the data type of variables and objects.
  • Debugging programs by verifying the type of stored values.
  • Validating input before performing operations.
  • Writing functions that behave differently for different data types.
  • Learning and exploring Python’s built-in data types.
  • Inspecting objects while testing and developing Python programs.

Key Takeaways: type() Function

Before wrapping up, here are the key points to remember about the Python type() Function:

  • The type() function returns the type (class) of an object.
  • It works with built-in data types as well as user-defined objects.
  • It helps identify the data type of variables, values, and objects.
  • The returned type can be displayed, compared, or used in program logic.
  • The three-argument form of type() can create classes dynamically.
  • It is widely used for debugging, testing, and understanding Python programs.

In short, the Python type() Function makes it easy to identify and work with different data types, helping you write more reliable Python programs.

Leave a Comment

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

Scroll to Top