Skip to content

Commit 2edc284

Browse files
committed
Add instructions for mutable and immutable objects in Python with examples
1 parent 2dd1812 commit 2edc284

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
191 KB
Loading

muteableVsimmutable/instructions.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Mutable vs Immutable Objects in Python
2+
3+
## What Are Objects in Python?
4+
5+
- Every variable in Python holds an instance of an object.
6+
- There are two main types of objects:
7+
- Mutable
8+
- Immutable
9+
- Each object has a unique ID.
10+
- The object type is defined at runtime and cannot be changed.
11+
- If the object is mutable, its internal state can be modified.
12+
13+
---
14+
15+
## Immutable Objects in Python
16+
17+
Immutable objects cannot be changed after creation.
18+
19+
**Examples of Immutable Types:**
20+
21+
- int
22+
- float
23+
- bool
24+
- str
25+
- tuple
26+
- unicode
27+
28+
### Example 1: Tuple (Immutable)
29+
30+
```python
31+
tuple1 = (0, 1, 2, 3)
32+
tuple1[0] = 4 # Error
33+
```
34+
35+
**Error:**
36+
37+
```
38+
TypeError: 'tuple' object does not support item assignment
39+
```
40+
41+
### Example 2: String (Immutable)
42+
43+
```python
44+
message = "Welcome to GeeksforGeeks"
45+
message[0] = 'p' # Error
46+
```
47+
48+
**Error:**
49+
50+
```
51+
TypeError: 'str' object does not support item assignment
52+
```
53+
54+
---
55+
56+
## Mutable Objects in Python
57+
58+
Mutable objects can be changed after creation.
59+
60+
**Examples of Mutable Types:**
61+
62+
- list
63+
- dict
64+
- set
65+
- custom classes
66+
67+
---
68+
69+
## Lists are Mutable
70+
71+
Lists allow modifications such as adding, removing, and changing elements.
72+
73+
### Example:
74+
75+
```python
76+
my_list = [1, 2, 3]
77+
my_list.append(4)
78+
my_list.insert(1, 5)
79+
my_list.remove(2)
80+
popped_element = my_list.pop(0)
81+
82+
print(my_list)
83+
print(popped_element)
84+
```
85+
86+
**Output:**
87+
88+
```
89+
[5, 3, 4]
90+
1
91+
```
92+
93+
---
94+
95+
## Dictionaries are Mutable
96+
97+
You can modify key-value pairs.
98+
99+
### Example:
100+
101+
```python
102+
my_dict = {"name": "Ram", "age": 25}
103+
new_dict = my_dict
104+
new_dict["age"] = 37
105+
106+
print(my_dict)
107+
print(new_dict)
108+
```
109+
110+
**Output:**
111+
112+
```
113+
{'name': 'Ram', 'age': 37}
114+
{'name': 'Ram', 'age': 37}
115+
```
116+
117+
---
118+
119+
## Sets are Mutable
120+
121+
You can add or remove elements from a set.
122+
123+
### Example:
124+
125+
```python
126+
my_set = {1, 2, 3}
127+
new_set = my_set
128+
new_set.add(4)
129+
130+
print(my_set)
131+
print(new_set)
132+
```
133+
134+
**Output:**
135+
136+
```
137+
{1, 2, 3, 4}
138+
{1, 2, 3, 4}
139+
```
140+
141+
---
142+
143+
## Special Case: Tuple with Mutable Objects
144+
145+
Tuples are immutable, but if they contain a mutable item (like a list), that item can still be changed.
146+
147+
### Example:
148+
149+
```python
150+
tup = ([3, 4, 5], 'myname')
151+
tup[0].append(6)
152+
print(tup)
153+
```
154+
155+
**Output:**
156+
157+
```
158+
([3, 4, 5, 6], 'myname')
159+
```
160+
161+
---
162+
163+
## Summary
164+
165+
- Immutable objects: faster access, but cannot be changed directly.
166+
- Mutable objects: can be modified easily.
167+
- Use mutable types when you need to change or update values.
168+
- Typically:
169+
170+
- Primitive types (int, str, float) → Immutable
171+
- Container types (list, dict, set) → Mutable

0 commit comments

Comments
 (0)