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 += 1 to modify an int in-place (ints are immutable).
  • Copying lists via assignment: use list(a), a.copy(), slicing, or copy module.

Checks for Understanding

  1. Is tuple mutable?
  2. What is the value of a after appending to b in the example above?
Show answers
  1. No, tuples are immutable.
  2. [1, 2, 3] because a and b reference the same list.

Exercises

  1. Create variables of each core type and print their types using type().
  2. Demonstrate the difference between re-binding an immutable value and mutating a list.