Python - Variables & Data Types
Overview
Estimated time: 25–35 minutes
Understand names and binding, dynamic typing, and Python's core built-in data types.
Learning Objectives
- Create and assign variables; understand that names bind to objects.
- Identify core types: int,float,str,bool,list,tuple,set,dict,None.
- Recognize mutability and its impact on behavior.
Prerequisites
Variables bind names to objects
x = 10         # name x binds to an int object
x = x + 5      # now binds to a new int object (ints are immutable)
items = [1, 2] # list is mutable
items.append(3)
print(x, items)
Expected Output: 15 [1, 2, 3]
Core data types
n: int = 123
pi: float = 3.14
ok: bool = True
s: str = "hello"
L: list[int] = [1, 2, 3]
T: tuple[int, ...] = (1, 2, 3)
S: set[int] = {1, 2, 3}
D: dict[str, int] = {"a": 1, "b": 2}
none_value = None
Mutability matters
a = [1, 2]
b = a          # b refers to the same list
b.append(3)
print(a)       # [1, 2, 3]
x = 10
y = x
y += 1
print(x)       # 10
Common Pitfalls
- Expecting y += 1to modify an int in-place (ints are immutable).
- Copying lists via assignment: use list(a),a.copy(), slicing, orcopymodule.
Checks for Understanding
- Is tuplemutable?
- What is the value of aafter appending tobin the example above?
Show answers
- No, tuples are immutable.
- [1, 2, 3]because- aand- breference the same list.
Exercises
- Create variables of each core type and print their types using type().
- Demonstrate the difference between re-binding an immutable value and mutating a list.