Python Data Types: Complete Built-in Types Hierarchy & Mutability Guide

Every computer program processes data. In Python, a data type defines the category of value an object holds, the operations that can be performed on it, and how it is stored in physical memory. Because Python is an object-oriented language, everything in Python is an object, and every object is an instance of a specific built-in data type class.

Complete Python Built-in Data Types Hierarchy Diagram

Figure 12.1: Complete architectural hierarchy of Python built-in data types categorized by mutability.


Checking Data Types with type() and isinstance()

Python provides two built-in functions to verify the data type of any variable:

# Inspecting types
data = [10, 20, 30]

print(type(data))                  # 
print(isinstance(data, list))      # True (Recommended: supports inheritance!)
print(isinstance(data, (list, tuple)))  # True (Checks if it matches either type)

Mutable vs Immutable Data Types (The BCA Golden Rule)

In university exams and developer interviews, the concept of mutability is paramount:

Feature Immutable Data Types Mutable Data Types
Definition Cannot be changed after creation. Any change creates a new object. Can be modified in place without changing its memory ID address.
Python Types int, float, complex, bool, str, tuple, frozenset list, dict, set, bytearray
Dictionary Keys Can safely be used as dictionary keys (hashable). Cannot be used as dictionary keys (raises TypeError: unhashable).
Performance Thread-safe, lower memory footprint, highly optimized. Efficient for fast in-place additions, deletions, and sorting.

Overview of the 6 Data Type Categories

• 1. Numeric Types (int, float, complex): Python 3 integers have arbitrary precision (unlimited digits limited only by available RAM).

• 2. String (str): Immutable sequence of Unicode characters. See our dedicated Python Strings Class.

• 3. List (list): Ordered, mutable sequence of mixed objects. See our dedicated Python Lists Class.

• 4. Tuple (tuple) & Set (set): Fixed sequences and unique unordered collections. See our Tuples & Sets Class.

• 5. Dictionary (dict): Ultra-fast key-value mapping backed by a C hash table. See our Dictionaries Class.

• 6. Boolean & NoneType (bool, None): Logical truth values and the singleton None.


Hands-on Practice Code: Mutability in Action

# Proving string immutability vs list mutability:
name = "Veewom"
print("Original string ID:", id(name))
name = name + ".com"
print("New string ID:     ", id(name))  # DIFFERENT MEMORY ADDRESS!

items = ["Keyboard", "Mouse"]
print("Original list ID:  ", id(items))
items.append("Monitor")
print("Updated list ID:   ", id(items))  # EXACT SAME MEMORY ADDRESS!

Frequently Asked Questions (FAQ)

Why is type checking with isinstance() preferred over type()?

isinstance(object, Class) supports object-oriented inheritance. If a class inherits from list, isinstance() returns True, whereas type() == list returns False.

What is the difference between a set and a frozenset?

A standard set is mutable (you can add and remove elements). A frozenset is immutable and frozen upon creation, making it hashable and usable as a dictionary key or an element inside another set.
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!