Lesson 04
Item Assignment and id() in Python
Understand why lst[0] = 99 works, why s[0] = P fails, and how id() proves whether Python modified an object or created a new one.
What you'll learn
| Concept | Focus |
|---|---|
| Item assignment | obj[i] = value |
TypeError |
Why immutable objects reject edits |
id() |
Same object vs new object |
| Rebinding | Changing the variable, not the object |
From Mutable Types, you know lists edit in place. This lesson separates item assignment from rebinding — and shows how id() proves which happened.
Big picture: two different kinds of assignment
Many beginners confuse these two operations.
Golden rule:
obj[index] = valueedits an object.name = valuechanges what the variable points to.
1. What is item assignment?
Item assignment means replacing one element inside an existing object.
Syntax:
object[index] = valueExamples:
lst[0] = 99
d["name"] = "Python"2. Mutable objects: same id()
Lists allow item assignment because they are mutable.
Visual proof
lst = [10, 20, 30]
print(id(lst))
lst[0] = 99
print(id(lst))Example output (your values will differ):
140200011111111
140200011111111The memory identity did not change.
3. Immutable objects: TypeError
Strings and tuples reject item assignment.
s = "abc"
s[0] = "A"Error:
TypeError: 'str' object does not support item assignmentTuple behaves the same:
t = (1, 2, 3)
t[0] = 9Rule: Immutable objects allow reading, not editing.
4. Rebinding vs in-place change
These two statements look similar — but behave differently.
In-place — lst[0] = 9
Rebinding — s = s.upper()
s = "python"
old = id(s)
s = "PYTHON"
print(id(s) != old)Output:
TrueThe variable s now points to a different string object.
5. id() vs is
id() gives the object's identity.
is checks whether two variables reference the same object.
a = [1, 2]
b = a
print(a is b) # True
b = [1, 2]
print(a is b) # FalseOutput:
True
FalseEqual values do not always mean the same object.
Related: is / is not
Complete comparison table
| Operation | Mutable | Immutable |
|---|---|---|
| Read item | Yes | Yes |
| Item assignment | Allowed | TypeError |
id() after edit |
Same | New object |
| Example | lst[0] = 99 |
s = s.upper() |
Playground
Experiment with both behaviors.
lst = [1, 2, 3]
print("list id:", id(lst))
lst[0] = 10
print("after lst[0]=10 id:", id(lst))
s = "abc"
try:
s[0] = "A"
except TypeError as e:
print("str item assign:", e)Observe:
- List → same ID
- String →
TypeError
Use the lesson playground at the bottom of the page to run this code live.
Quick knowledge check
-
Why does
s[0] = "P"fail?
Answer: Strings are immutable and do not support item assignment. -
Does
lst[0] = 1create a new list?
Answer: No — it modifies the existing list, soid(lst)stays the same. -
What's the difference between these two?
s = s.upper()
s[0] = "P"s = s.upper()→ creates a new string and rebindsss[0] = "P"→ tries to edit the old string and raisesTypeError
30-second interview revision
| Concept | Remember |
|---|---|
| Item assignment | Edits an existing object |
| Mutable | Same id() |
| Immutable | Raises TypeError |
| Rebinding | Variable points to a new object |
is |
Same object? |
id() |
Object identity |
Interview shortcut: lst[0] = 99 changes the object. s = s.upper() changes the variable's reference, not the original string.
Next: Aliasing and Copying.
Loading playground…