Could someone explain in a simple way please


#1



I'm on the "More with 'for'" section of 'Python List and Dictionaries' and I must say, the way codecademy explains some areas are rather poor and confusing.

It took me a while to under stand how to figure it out, but I have no idea how what I did fixed the problem.

The code I originally used was :

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

for number in start_list:
    start_list.append(number ** 2)
    start_list.sort()


print square_list

What it should have been is :

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

for number in start_list:
    square_list.append(number ** 2)
    square_list.sort()


print square_list


Can someone explain how using 'square_list.append(number **2) works when there is no information in square_list.

I gone back to previous lessons and to try and understand but codecademy doesn't really explain how this functions work.

Thanks


#3

Well the .append function adds something that you tell it to to a list you tell it to in this case: squarelist.

it doesnt matter if there is anything in the list. Think of the list as empty basket. With the .append you add the number**2 to the basket. Equivalent of adding an apple to the empty basket


#4

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