Guys please help me to understand this weird behaviour that I managed to output.
>>> a = [1, 4, 3, 2]
>>> a
[1, 4, 3, 2]
>>> b = a.sort()
>>> a
[1, 2, 3, 4]
>>>
Why would "b = a.sort()" sort list 'a'?
a.sort() sorts a IN-PLACE. This means that the contents of a are altered.
Further, b will be None
I think what you are looking for is b = sorted(a), which creates a sorted copy of the contents of a and stores it in b
>>> a = [1,4,3,2]
>>> b = sorted(a)
>>> a
[1, 4, 3, 2]
>>> b
[1, 2, 3, 4]
>>> b = a.sort()
>>> a
[1, 2, 3, 4]
>>> b
>>>
a.sort() will sort a and assign the sorted contents of a back to a (this is kind of what sorting IN-PLACE means). There is therefore, no return value (this is the same as a return value of None. So a.sort() is called, which sorts a IN-PLACE, and takes the return value of such sorting (which is None) and assigns that return value to b (therefore b is None).
If you were to do b=a.count(2), a.count(2) is called, which returns an integer (in this case 1). This return value is assigned to b, which is how b comes to contain the value 2.
>>> a = [1,4,3,2]
>>> a.count(2)
1
>>> b = a.count(2)
>>> b
1
>>>
Similarly, when you do b=a.sort(), a.sort() is called (the call to a.sort() sorts the items in a IN-PLACE), which returns None. This return value is assigned to b.
Now, in the above example, a.count(2) still counted the number of occurrences of 2 in a, despite the fact that the return value of calling a.count(2) was not assigned to anything. This is the exact logic behind why a.sort() sorts a, even though the result is not assigned to anything