CodeBlanks Logo Icon CodeBlanks

id and type

Learn object identity and type with id() and type().

What is id?

Every object in Python has a unique identity. The id() function returns it:

x = 5
print(id(x))  # example: 140234567890

The identity is the address of the object in memory. Two objects with the same value may have different identities. But for small integers Python shares them (interning).

The is operator checks whether two variables point to the same object by identity.

type vs isinstance

type(x) returns the type object of the variable. isinstance(x, int) returns True/False whether x is of that type.

type(5) == int            # True
isinstance(5, int)        # True

isinstance also considers inheritance, so it is preferred for type checks. type is used when exact type equality is needed.

Practice Print the type

Assign 5 to x. Then print its type with print(type(x)).

Starter code

main.py
# Print the type

Sample solution

main.py
x = 5
print(type(x))

Expected output

<class 'int'>

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does type(5) return?

  • 5
  • int
  • <class 'int'> Correct answer
  • integer

Explanation: type(5) returns <class 'int'>.

2 Check the type

Question
type(x) == ____
Answer
type(x) == int

Explanation: type(x) == int checks the type.

3 Match the concepts

Term Meaning
id() Object identity
type() Type object
isinstance() Inheritance-aware check
is Identity comparison

All lessons in Beginner