Why executing the following:
while executing the following:
a = [7,1,0] a.sort() print(a)
[0, 1, 7]
.sort() operates on the list and returns
print(sorted(a)) prints what you want but does not alter the list
sort() sorts the list but returns
None. You probably want
.sort() method sorts the list in place and returns
None. The list becomes sorted, which is why printing the list displays the expected value. However,
print(x.sort()) prints the result of
Because in the first example
print([7,1,0].sort()) you are printing value returned by
sort() does not return anything.
While later you are performing
sort() on the list
a and then printing the list
Sort() changes order of elements in the list
a and does not return anything.
sorted() to get sorted list as returned value.