Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. len() / type() / id()

len() / type() / id()

Since: Python 2(2000)

These are basic built-in functions for checking the length, type, and memory identity of objects. They are frequently used for debugging and type checking.

Syntax

# Returns the number of elements (length) of an object.
len(object)

# Returns the type of an object.
type(object)

# Returns the memory identity (an integer) of an object.
id(object)

# Checks whether an object is an instance of the specified type.
isinstance(object, type)

# Checks whether a class is a subclass of the specified class.
issubclass(subclass, parent_class)

Function List

FunctionDescription
len(obj)Returns the number of elements in an object such as a list, string, or dictionary as an integer.
type(obj)Returns the type (class) of an object. The return value is a type object.
id(obj)Returns a unique integer identifier for the object in memory. Useful for checking whether two variables refer to the same object.
isinstance(obj, classinfo)Returns True if the object is an instance of the specified type or a subclass of that type.
issubclass(class, classinfo)Returns True if the first argument class is a subclass of the second argument class.

Sample Code

sample_len_type_id.py
# Use len() to get the length of various objects.
print(len("hello"))          # 5 (number of characters)
print(len([1, 2, 3, 4]))     # 4 (number of list elements)
print(len({"a": 1, "b": 2})) # 2 (number of dictionary keys)
print(len((10, 20, 30)))     # 3 (number of tuple elements)

# Use type() to check the type of an object.
x = 42
y = "hello"
z = [1, 2, 3]
print(type(x))   # <class 'int'>
print(type(y))   # <class 'str'>
print(type(z))   # <class 'list'>

# Use id() to check the identity of an object.
a = [1, 2, 3]
b = a          # refers to the same object
c = [1, 2, 3]  # a different object
print(id(a) == id(b))  # True (same object)
print(id(a) == id(c))  # False (different objects)

# Use isinstance() to check the type of a value.
num = 3.14
print(isinstance(num, float))        # True
print(isinstance(num, (int, float))) # True (multiple types can be specified)
print(isinstance(num, str))          # False

# Use issubclass() to check the inheritance relationship between classes.
print(issubclass(bool, int))   # True (bool is a subclass of int)
print(issubclass(int, float))  # False
python3 len_type_id.py
5
4
2
3
<class 'int'>
<class 'str'>
<class 'list'>
True
False
True
True
False
True
False

Notes

len() works with all of Python's built-in sequence types, including lists, strings, tuples, dictionaries, and sets. To use it with a custom class, implement the __len__() method in that class.

type() returns the type of an object when called with one argument, and dynamically creates a new class when called with three arguments. For type comparisons, it is recommended to use isinstance(x, int) rather than type(x) == int. isinstance() is more flexible because it also matches subclasses.

id() returns a unique integer that corresponds to the object's memory address. In Python, small integers and short strings are interned (cached), so they may unexpectedly share the same ID. When you need to check object identity, using the is operator is clearer. For type conversion, see int() / float() / str() / bool().

If you find any errors or copyright issues, please .