.sort()


#1



https://www.codecademy.com/en/courses/python-beginner-en-pwmb1/1/6?curriculum_id=4f89dab3d788890003000096#


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

for number in start_list:
squared_number = number**2
print squared_number
square_list.append(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.


#2

suppose you sort a list..

say..

x = [2,5,1]

#then you try to do this..

print x.sort()

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

.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..

x.sort()

then print..

print x
It will print in sorted manner!


#3

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