9. More with for: line by line explanation


#1

Hi,

I want to make sure I understand what each line is doing in the solution to this exercise as it was a challenge for me. The code is in bold, my reasoning is italicized. I would appreciate any correction and/or annotation to my reasoning.

start_list = [5, 3, 1, 2, 4] This creates a list named "start_list" that contains numbers 5,3,1,2 and 4
square_list = [] This creates a second list with nothing in it thus far

for number in start_list: this is the for loop that says for every number in the list named start list, do whatever is written on the next line...

print number this prints out each number as it goes through the for loop in the console

square_list.append(number**2) this adds to the list named square list whatever number prints out to the second power

square_list.sort() this sorts the list named square list numerically

print square_list this prints out the list named square list in the console


#2

This all sounds correct to me. If a Python guru wanders by they may have some more in depth comments.