The 9th lesson of Python tutorial_Iterable, mutable and immutable objects - BoardCode

The 9th lesson of Python tutorial_Iterable, mutable and immutable objects

Share This

The 9th  lesson of Python tutorial_Iterable, mutable and immutable objects
In previous lessons we've talked about stringsstrings methods, lists and lists methods, there, we've  said strings are immutable objects and lists are mutable objects and both of them are iterables but the question is what's an iterable, mutable and an immutable object? And what's the difference between mutable and immutable objects? In this post we will discuss all of this.
As said in the first lesson, any object  has three important properties which are a name,  value and  type. An object is mutable or immutable depends on  its ability to change its value without changing its position in the memory. By the built-in function id(), Python provides the ability to check out an object id in the memory.
Simply, an object is immutable if it can't change its value without changing its id

Examples:
>>> string='Python'
if we try to change string's value we will get an error.
>>>  string[0]='J'
 Traceback (most recent call last):
  File "", line 1, in
    string[0]='J'
TypeError: 'str' object does not support item assignment

Even if we assign string's value to another variable the new one will be the same
just it will consider as another name of string
Example:
>>> string2 = string
>>> string2 is string
True
The is statement returns True if two variables have the same identity, otherwise False

>>> id(string) == id(string2)
 True
Now, if we want to change string's value (in general any immutable object) we need create another object and do some changes. For example if we want string2 holds 'Jython' we will do changes as follow.

>>> string2 = string[1:]
>>> string2
'ython'
>>> string is string2
False
>>> string2 ='J' +string2
>>> string2
'Jython'
>>> string2 is string
False

Here the string2 is different.
Briefly, an immutable object can't change its value and keeps its identity.
The built-in immutable types in Python are str, int, float, tuple, bool and set.
  




The mutable is opposite, it can change its value and keeps its identity. An example of mutable is list.

>>> mylist=['P','y','t','h','o','n']
>>> id(mylist)
3053712204
>>> identity=id(mylist)
>>> mylist[0]='J'
>>> mylist
['J', 'y', 't', 'h', 'o', 'n']

>>> identity == id(mylist)
True 


 As we can see, when we tried to modify the first item of string we got an error unlike mylist that's been done successfully.
The buit-in mutable types in Python  are list, dictionary.


Briefly, a mutable can change its value and keeps its identity.

Now, let's turn to iterable objects.
Simply, an iterable is an object ca return its members one by one, in other words, we can loop through it by using for or while loops.

>>> mylist=['P','y','t','h','o','n']
>>> for i in mylist:
    print(i)

P
y
t
h
o
n


The iterable built-in types in Python are string, list, tuple, set, dictionary, and generators, some of these types have already been discussed  and others  will be later.


No comments:

Post a Comment

Pages