More with For ( in Python )


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

Your code here!

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

print square_list

(1)Why do I need “square list=[]” here? What does it perform? The code does not run correctly if I delete all the square_list related lines, I don’t get a right result…
(2) May I understand I can assign any name I like to the word that comes after “for” because it is a variable’s name?


#1 The square_list = [] here basically just says that square_list is an empty array (for now). You need to create it to append (add) the results to the array.

#2 You can technically change it to whatever you want, you just need to keep it true throughout the entire loop.

If that doesn’t make sense, basically you could do this, you just need to make sure you use the same variable name.

for var_you_choose in start_list:
    square_list.append(var_you_choose ** 2)


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