Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner15 min

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.

Item assignment edits object same id vs Rebinding s=s.upper() new object new id

Golden rule: obj[index] = value edits an object. name = value changes what the variable points to.

1. What is item assignment?

Item assignment means replacing one element inside an existing object.

Syntax:

python
object[index] = value

Examples:

python
lst[0] = 99

d["name"] = "Python"
Only one slot changes in list 10 20 30 becomes 99 20 30

2. Mutable objects: same id()

Lists allow item assignment because they are mutable.

Visual proof

id=8001 before [10,20,30] lst[0]=99 after [99,20,30] same id
python
lst = [10, 20, 30]

print(id(lst))

lst[0] = 99

print(id(lst))

Example output (your values will differ):

text
140200011111111
140200011111111

The memory identity did not change.

3. Immutable objects: TypeError

Strings and tuples reject item assignment.

abc string s[0]=A raises TypeError
python
s = "abc"

s[0] = "A"

Error:

text
TypeError: 'str' object does not support item assignment

Tuple behaves the same:

python
t = (1, 2, 3)

t[0] = 9

Rule: 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

In-place id A [1,2] becomes [9,2] same box

Rebinding — s = s.upper()

Rebinding A arrow to new object B
python
s = "python"

old = id(s)

s = "PYTHON"

print(id(s) != old)

Output:

text
True

The 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 and b alias same [1,2] object; rebinding b creates new list
python
a = [1, 2]

b = a

print(a is b)      # True

b = [1, 2]

print(a is b)      # False

Output:

text
True
False

Equal 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.

python
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

  1. Why does s[0] = "P" fail?
    Answer: Strings are immutable and do not support item assignment.

  2. Does lst[0] = 1 create a new list?
    Answer: No — it modifies the existing list, so id(lst) stays the same.

  3. What's the difference between these two?

python
s = s.upper()
s[0] = "P"
  • s = s.upper() → creates a new string and rebinds s
  • s[0] = "P" → tries to edit the old string and raises TypeError

30-second interview revision

obj[i]=value decision tree — Mutable lst[0]=99 same id vs Immutable TypeError; Rebinding new id
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…