Data Types
Topic — Python organizes data into four scalar types (holding atomic values:
int,float,str,bool) and four collection types (holding multiple elements:list,tuple,dict,set). Understanding the structural properties of each—specifically mutability, ordering, subscript indexing, and hashability—is essential for designing reliable software and efficient data pipelines.
1. The Core Concept
Every built-in Python data type is governed by three fundamental design questions:
- Is it Scalar or Collection? Does it represent a single atomic value (
int) or a container of elements (list)? - Is it Mutable or Immutable? Can the object be modified in-place at its existing memory address, or does modification allocate a new object?
- Is it Ordered or Unordered? Do elements maintain deterministic sequence positions accessible by index (
[0]), or are they hashed sets?
The 8 Foundational Types Overview
| Category | Type | Literal Example | Mutability | Ordering | Access Method |
|---|---|---|---|---|---|
| Scalar | int | 28 | Immutable | — | Direct scalar |
| Scalar | float | 15.75 | Immutable | — | Direct scalar |
| Scalar | str | "Vettri" | Immutable | Ordered | By Index (s[0]) |
| Scalar | bool | True | Immutable | — | Logical flag |
| Collection | list | [1, 2, 3] | Mutable | Ordered | By Index (l[0]) |
| Collection | tuple | (1, 2, 3) | Immutable | Ordered | By Index (t[0]) |
| Collection | dict | {"id": 101} | Mutable | Ordered (Keys unique) | By Key (d["id"]) |
| Collection | set | {1, 2, 3} | Mutable | Unordered | Membership only (in) |
If you only take one thing from this page
- Use
listwhen you need an ordered sequence that will change. - Use
tuplewhen you need an ordered record that must remain protected and hashable. - Use
dictfor lightning-fast key-value lookups. - Use
setfor unique collections and mathematical set operations (union, intersection).
2. Interactive Data Type Taxonomy Explorer
Filter and inspect the structural metadata, syntax rules, and gotchas across all 8 core types:
Interactive Python Data Type Taxonomy
Click any data type to inspect its structural blueprint and common gotchas:
list)fruits = ["apple", "banana"]Zero-based Index (fruits[0])
Duplicates allowed
3. Interactive Mutability & Hashability Sandbox
Observe what happens when you attempt to modify objects in-place or pass mutable objects as dictionary keys:
In-Place Mutability and Hashability Sandbox
Select an operation scenario to inspect Python runtime memory responses:
fruits = ["apple", "banana"]
fruits[0] = "pear" # In-place index reassignment
fruits.append("grape") # In-place extension
print(fruits) # ['pear', 'banana', 'grape']Mechanism: The list object expands and modifies internal item pointers in-place. The outer memory address (id(fruits)) remains completely unchanged.
4. The Four Collection Types in Detail
- 1. list — [1, 2, 3]
- 2. tuple — (1, 2, 3)
- 3. dict — {'a': 1}
- 4. set — {1, 2, 3}
- Syntax: Square brackets
[]. - Properties: Ordered sequence, supports mixed types, allows duplicates, mutable.
- Indexing & Slicing:
nums = [10, 20, 30, 40]nums[0] # 10 (First)nums[-1] # 40 (Last)nums[1:3] # [20, 30] (Stop index excluded)
- Common Methods:
.append(x),.insert(i, x),.pop(),.remove(x).
- Syntax: Parentheses
(). - Trailing Comma Rule:
(42,)is a 1-element tuple.(42)is an integer! - Properties: Ordered sequence, immutable, hashable (can be a dict key).
- Why use tuple? Faster access, smaller memory footprint, protects data from accidental modification.
- Syntax: Key-value pairs inside curly braces
{key: value}. - Properties: Fast average lookup by key, mutable, preserves insertion order (since Python 3.7).
- Safe Retrieval:
user = {"name": "Alex", "age": 28}user["salary"] # KeyError!user.get("salary", 50000) # Returns 50000 (safe default)
- Syntax: Unique elements inside curly braces
{1, 2, 3}orset(). - Empty Set Trap:
{}creates an emptydict, not a set! To make an empty set, you must callset(). - Properties: Unordered (no indexing
s[0]), duplicates automatically stripped, elements must be hashable. - Mathematical Set Operations:
A = {1, 2, 3}B = {3, 4, 5}A | B # {1, 2, 3, 4, 5} (Union)A & B # {3} (Intersection)A - B # {1, 2} (Difference)A ^ B # {1, 2, 4, 5} (Symmetric Difference)
5. Implementation Lab
Lab Exercise: Data Types, Collections & Hashability
An interactive, fully functional Google Colab / Jupyter Notebook is available to inspect all 8 types, test in-place mutability, reproduce the shallow tuple trap, and evaluate dictionary key hashability live in your browser.
How to run the lab:
- Click the "Open In Colab" badge above to launch the interactive notebook.
- Run cells sequentially to benchmark mutability, hashability errors, and set operations.
Key Experiments to Run:
- Experiment 1 (In-Place Mutability with id()): Modify a list at index 0 and verify that
hex(id(my_list))remains unchanged before and after mutation. - Experiment 2 (The Shallow Tuple Test): Append an integer to a list nested inside a tuple (
t[1].append(999)) to witness nested mutation without breaking the outer tuple. - Experiment 3 (Testing Key Hashability): Verify that tuples work as dictionary keys (
d[(10, 20)]), while lists raiseTypeError: unhashable type: 'list'.
6. Common Mistakes
- 1. Single-Element Tuple Comma
- 2. The Empty Set Trap
- 3. Attempting to Index a Set
- 4. Using Mutable Objects as Dict Keys
- 5. Sets Absorbing Booleans
- The Misconception: "Parentheses alone create a tuple, so
x = (42)is a single-element tuple." - Wrong Thinking: Typing
x = (42). - The Right Principle: Parentheses are also used for arithmetic grouping. Without a trailing comma,
(42)is evaluated as integer42. You must writex = (42,).
- The Misconception: "To create an empty set, type
s = {}." - Wrong Thinking:
type({})is<class 'dict'>. - The Right Principle:
{}was reserved for dictionaries historically. Callingtype({})returnsdict. To create an empty set, call the constructor:s = set().
- The Misconception: "I can access the first element of a set using
my_set[0]." - Wrong Thinking: Treating sets like lists.
- The Right Principle: Sets are unordered hash tables without sequence indices.
my_set[0]raisesTypeError: 'set' object is not subscriptable. Usefor item in my_set:or convert vialist(my_set)[0].
- The Misconception: "I can use a list as a key in a dictionary to look up coordinates."
- Wrong Thinking:
d[[10, 20]] = "Home". - The Right Principle: Dictionary keys must be hashable. Lists are mutable and unhashable, raising
TypeError: unhashable type: 'list'. Use an immutable tuple instead:d[(10, 20)] = "Home".
- The Misconception: "A set containing
1andTruewill have length 2." - Wrong Thinking: Expecting type distinction in sets for
1andTrue. - The Right Principle: Because
boolinherits fromint,True == 1andhash(True) == hash(1). The set treats them as duplicates and retains only one:len({1, True}) == 1.
7. Summary
Four Scalar Primitives
- int: Arbitrary precision. No decimal. Immutable.
- float: IEEE 754 64-bit float. Approximate decimals. Immutable.
- str: Immutable text sequence. Slicing returns new strings.
- bool: Subclass of
int.True == 1,False == 0.
Four Collections
- list
[]: Ordered, mutable, allows duplicates. Indexing[0]. - tuple
(): Ordered, immutable, hashable. Needs comma(42,). - dict
{}: Key-value mapping. Keys must be unique and hashable. - set
{}: Unordered, unique, mutable. Empty set isset().
Key Takeaways
- Mutability dictates safety: Lists and dicts can be modified in-place; tuples and strings cannot.
- Hashability requires immutability: Only immutable types (integers, strings, tuples) can be used as dictionary keys or set elements.
- Shallow immutability: A tuple containing a mutable list can have its inner list modified in-place.
Next Section: Expressions and Operators - mastering arithmetic, comparison, logical, and bitwise operators in Python.
8. Active Recall
Test your understanding by answering first, then clicking to reveal the underlying principles.
Interactive Checkpoint Quiz
Interactive Checkpoint: The Shallow Tuple Mutation
Consider this Python snippet: t = ("data", [1, 2]); t[1].append(3); print(t). What happens when this code executes?
Review Flashcards
1. [THEORY] Why can't a list be used as a key in a Python dictionary?
- The Hashability Invariant: Python dictionaries are hash tables. When a key is inserted, Python computes its hash integer:
hash(key). - If a key could be mutated in-place (e.g.
key.append(5)), its hash integer would change, making the entry impossible to find in its original hash bucket. - To prevent silent data corruption, Python enforces that all dictionary keys must be immutable and hashable. Passing a list raises
TypeError: unhashable type: 'list'.
2. [MATH] Why does len({1, True, 1.0}) evaluate to 1 instead of 3?
- Value Equality and Matching Hashes: In Python:
- Because sets strictly eliminate duplicate elements that share both equal value and identical hash values,
Trueand1.0are absorbed by the integer1. - The set contains only a single element, so its length is
1.
3. [PRACTICE] What is the difference between dict["key"] and dict.get("key")?
dict["key"]attempts direct lookup. If the key is missing, it raises an unhandledKeyErrorexception.dict.get("key")safely returnsNoneif the key is missing.dict.get("key", default)allows specifying a fallback return value (e.g.d.get("count", 0)), preventing crashes without requiring explicitif key in d:checks.
4. [THEORY] Why is x = (42) an integer, while x = (42,) is a tuple?
- Grammar Disambiguation: In Python syntax, parentheses
()serve dual roles:- Expression grouping:
(2 + 3) * 4 - Tuple construction:
(1, 2)
- Expression grouping:
- If
(42)were parsed as a tuple, mathematical grouping would be impossible. - The Python grammar specifies that the comma
,is what constructs a tuple, not the parentheses! The trailing comma in(42,)flags it as a tuple.
5. [ANALYZE] How do you create an empty set in Python, and why does {} not work?
- To create an empty set: Call the constructor function:
s = set(). - Why
{}fails: Dictionaries were introduced in Python long before sets were added. Curly braces{}were already reserved to create an empty dictionary (type({}) == dict). - Writing
{}creates an empty dictionary, not an empty set.