├── Assignment-3.txt
└── README.md
/Assignment-3.txt:
--------------------------------------------------------------------------------
1 | # Python3
2 | Show that List and Dictionaries are mutable in nature.
3 | #Mutable means we can changed (“mutated”).
4 | #An immutable means is read-only; it cannot be changed.
5 | >>>a=1
6 | >>>b=a
7 | >>> id(a)
8 | 4547990592
9 | >>> id(b)
10 | 4547990592
11 | >>> id(1)
12 | 4547990592
13 | #Notice id() for all of them is same. id() basically returns an integer which corresponds to the object’s memory location.(b → a → 1 → 4547990592)
14 | Now, let’s increment the value of “a” by 1 so that we have new integer object → 2.
15 | >>> a=a+1
16 | >>> id(a)
17 | 4547990624
18 | >>> id(b)
19 | 4547990592
20 | >>> id(2)
21 | 4547990624
22 | #Notice how id() of variable “a” changed, “b” and “1” are still same.
23 | b → 1 → 4547990592
24 | a → 2 → 4547990624
25 | #The location to which “a” was pointing has changed ( from 4547990592 → 4547990624). Object “1” was never modified. Immutable objects doesn’t allow modification after creation. If you create n different integer values, each will have a different constant hash value.
26 | but in Mutable objects can change their value but keep their id() like list, dictionary,set.
27 | #LIST
28 | >>> x=[1,2,3]
29 | >>> y=x
30 | >>> id(x)
31 | 4583258952
32 | >>> id(y)
33 | 4583258952
34 | y → x → [1,2,3] → 4583258952
35 | Now, lets change the list
36 | >>> x.pop()
37 | >>> x
38 | [1,2]
39 | >>> id(x)
40 | 4583258952
41 | >>> id(y)
42 | 4583258952
43 | y → x → [1,2] → 4583258952
44 | #x and y are still pointing to the same memory location even after actual list is changed.
45 | >>>>>> x={1,2,3}
46 | >>> id(x)
47 | 66862184
48 | #DICTIONARIES
49 | >>> k={1:'English',2:'Hindi',3:'Bangali',4:'Tamil',5:'MIL'}
50 | >>> k
51 | {1: 'English', 2: 'Hindi', 3: 'Bangali', 4: 'Tamil', 5: 'MIL'}
52 | >>> id(k)
53 | 66294600
54 | >>> k[0]='computer'
55 | >>> k
56 | {1: 'English', 2: 'Hindi', 3: 'Bangali', 4: 'Tamil', 5: 'MIL', 0: 'computer'}
57 | >>> id(k)
58 | 66294600
59 | >>> k.pop(0)
60 | 'computer'
61 | >>> id(k)
62 | 66294600
63 | >>>
64 | #Here,we can see from the above example that whenever we changes the values in List and Dictionaries but they pointing to the same memory location even after actual list and dictionaries were changed.
65 | #Hence proved that List and Dictionaries are Mutable in Nature.
66 | Thank You :)
67 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # This repo is only for showing the difference between list and disctionaries.
2 | ### Show that List and Dictionaries are mutable in nature.
3 | Difference between mutable and immutable
4 |
--------------------------------------------------------------------------------