More with 'for' - question


#1

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


Hello. I've noticed interesting thing in exersice "More with 'for'". When my code looks such way, it works:


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

# Your code here!
for x in start_list:
    square_list.append(x**2)

square_list.sort()
print square_list


And that is the result:
[1, 4, 9, 16, 25]
None

But when I tried this:

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

# Your code here!
for x in start_list:
    square_list.append(x**2)

print square_list.sort()

I've had
"None
None"
in console and "Congratulations, you've finished this section!" message.

So I have questions about second variant. Why list wasn't printed? Was it sorted in fact?
P.S. There was no "print" requirement in Instructions, I just wanted to see result of my tries; the Instruction was literally "sort square_list"


#2

because sort() sorts the list, in essence updating it. Updating a variable and printing on the same line:

x = 5
print x = x + 5

is too much to do on a single line


#3

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