Explanation:
๐น Line 1: Create a Tuple
x = (1, 2, 3)
A tuple is created and stored in variable x.
Current value:
(1, 2, 3)
Memory:
Index Value
----- -----
0 1
1 2
2 3
๐น What is a Tuple?
A tuple is an immutable sequence.
Immutable means:
Cannot be changed after creation
Examples of immutable types:
tuple
str
frozenset
Examples of mutable types:
list
dict
set
๐น Line 2: Try to Change First Element
x[0] = 10
Python tries to replace:
1
with
10
at index:
0
Visual:
Before:
(1, 2, 3)
↑
index 0
Attempt:
(10, 2, 3)
๐น Why Does Python Reject This?
Because tuples are immutable.
Once created:
(1, 2, 3)
cannot become:
(10, 2, 3)
Python immediately stops execution.
๐น Error Raised
Python throws:
TypeError

0 Comments:
Post a Comment