+ 6
Why the output is None?
7 Antworten
+ 7
You should use print(sorted(a)).
a.sort() just executes the sorting, it does not return any value, that's why None is printed.
+ 6
Here it proves people of this community.
Likes only the answers of experienced person
+ 6
@Keep in mind. Your answer wouldnt have gotten as much exposure as Kubas as it was a good 10 hours after the question was asked.
That said, search likestorms here in the q and a.
+ 6
On top what @Keep wrote, you may try the below to see what the difference is between .sort() and sorted()
a=[4,2,6,1]
print(a)
a.sort()
print(a)
print()
b=[4,2,6,1]
print(b)
print(sorted(b))
As you can see .sort does its job in-place, but returns None. sorted(b) returns the sorted list, but does not change the list itself.
+ 6
Yes right sir I get it thanks.
+ 5
You should do like this.
a=[4,2,6,1]
a.sort()
print (a)
Now it will print the sorted no
+ 5
Thanks #sir kuba
My answer is also correct