Why the code in description giving "None" as a output ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why the code in description giving "None" as a output ?

a = [2,3,8,1,4] print(a.sort())

8th Nov 2020, 10:43 AM
Naseem Ullah
Naseem Ullah - avatar
4 Answers
+ 7
because a.sort() sorts the elements in place and doesn't returns a new list or anything so printing it out returns None as nothing returned by calling that sort method
8th Nov 2020, 10:46 AM
Abhay
Abhay - avatar
+ 6
It is because the sort() modifies the list in place and doesn't return anything. You could say- a.sort() print(a) OR print(sorted(a))
8th Nov 2020, 10:46 AM
Avinesh
Avinesh - avatar
+ 5
It's analogous to a.reverse() and reversed(a)
8th Nov 2020, 1:38 PM
David Ashton
David Ashton - avatar
+ 1
Avinesh what is function of sorted() ?
8th Nov 2020, 10:48 AM
Naseem Ullah
Naseem Ullah - avatar