start_list = [5, 3, 1, 2, 4]
square_list = []

for number in start_list:
squared_number = number**2
print squared_number
print square_list.sort()

Can anyone help me explain why print square_list.sort() does not work? I mean the output is said 'None'.

Instead, if I put 'square_list.sort() first, then print square_list, the output is a sorted list.

Replace this line with your code.


suppose you sort a list..


x = [2,5,1]

#then you try to do this..

print x.sort()

what are you expecting ?
does it print list in sorted order?

.sort() just sorts a list and returns None
Key thing to understand here is ,.sort() does not return the sorted list but it returns None!

so if you try to do this..


then print..

print x
It will print in sorted manner!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.