Python Reference Library →

Python type() Function – Get the Type of an Object

Let’s look at type() function – get the type of an object

  1. Notice

    type(value) returns the object’s type. A name can be rebound to an object of a different type.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)
Live state

Top-level scope

value→Object 2
Object 2 · str'3'
This step

type(value) returns the object’s type. A name can be rebound to an object of a different type.

Picture type() function – get the type of an object

type(value) returns the object’s type. A name can be rebound to an object of a different type.

Image brief

Alt text
A diagram for Python type() Function – Get the Type of an Object. Instructions: value = 3 print(type(value).__name__) value = str(value) print(type(value).__name__). Displayed result: int str. The final example bindings are value --> object 2 (str). object 2: '3'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python type() Function – Get the Type of an Object". Teaching focus: type(value) returns the object’s type. A name can be rebound to an object of a different type. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS value = 3 print(type(value).__name__) value = str(value) print(type(value).__name__) | v DISPLAYED RESULT int str FINAL BINDINGS value --> object 2 (str) object 2: '3' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python type() Function – Get the Type of an Object. Instructions: value = 3 print(type(value).__name__) value = str(value) print(type(value).__name__). Displayed result: int str. The final example bindings are value --> object 2 (str). object 2: '3'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)
Live state

Top-level scope

value→Object 2
Object 2 · str'3'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
int
str

What do you expect this program to print?

What does this example display?

value = 3
print(type(value).__name__)
value = str(value)
print(type(value).__name__)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

int str

Follow the printed values in order. The output is: int str

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

type(value) returns the object’s type. A name can be rebound to an object of a different type.

type(value) returns the object’s type. A name can be rebound to an object of a different type.

More examples and reference

Python type() Function

The type() function in Python is used to check the type of an object. It can also be used to create new types dynamically, though the most common use is checking the type of a variable.

Syntax

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

Parameters:

  • object – The object whose type you want to check.
  • name – (Advanced) Name of the new class to create.
  • bases – (Advanced) Tuple of parent classes.
  • dict – (Advanced) Dictionary of attributes and methods for the new class.

Returns:

  • For one argument: returns the type of the object.
  • For three arguments: returns a new type (class).

Example: Get Type of a Variable

x = 10
print(type(x))
<class 'int'>

Example: Type of Different Data Types

print(type(3.14))        # float
print(type("hello"))     # str
print(type([1, 2, 3]))    # list
print(type({ "a": 1 }))   # dict
<class 'float'>
<class 'str'>
<class 'list'>
<class 'dict'>

Use Case: Type Checking in Conditional Statements

value = "123"
if type(value) == str:
    print("It's a string!")
It's a string!

Advanced Use: Creating a Class Using type()

MyClass = type('MyClass', (), {'x': 5})
obj = MyClass()
print(obj.x)
5

In this form, type() is a metaclass constructor. It dynamically creates a class with name MyClass and an attribute x = 5.

Common Mistakes

  • Don't compare with 'int' or 'str' as strings. Use int, str, etc., directly.
  • Use isinstance() for safer type checking, especially with inheritance.

Interview Tip

type() is often used to check inputs in functions or debug issues. Knowing how it works helps in understanding Python's dynamic typing.

Summary

  • type(object) returns the class type of the object.
  • type(name, bases, dict) dynamically creates a new class.
  • Prefer isinstance() over type() when checking types in practice.

Practice Problem

Write a Python program that asks the user for input and prints the type of the entered value after converting it to int, float, or str.

value = input("Enter something: ")
try:
    print(type(int(value)))
except:
    try:
        print(type(float(value)))
    except:
        print(type(value))

What to remember

Connect what you noticed in the example with the idea below.

Remember

type(value) returns the object’s type. A name can be rebound to an object of a different type.

Watch out for this idea

type() changes the value into a different type.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →