More with 'for'


I don't understand this, I'm more confused about the for-loop part.

Replace this line with your code.


Recall that a for loop can be used to iterate items in a list, keys in a dictionary, or indices of a list using the range() function.

for item in start_list:

item will be one of the numbers.

for i in range(len(start_list)):

i will be an index, starting at 0 and continuing up to 4. start_list[i] will be the number at the position in the list.


I understand the formatting but I don't know what to put in


Using one or the other for loop, iterate the start_list and append the squared value of each item to the squares_list.


I really dont understand what the .append() is, it never says it prior, and I also dont understand what you mean when you say "iterate" and "append"


Iteration is the process of cycling through a set of data, one item at a time in the example,

for item in x:

or one index at a time, in sequence, in the example,

for i in range(len(x)):

append is a list method that adds an element to the end of the list and populates it with the given value.

    my_list = []
    my_list.append('first element')
    print my_list
    # [ 'first element' ]

Example using in

my_list = [ 'one', 'two', 'three', 'four' ]
for item in my_list:
    print item

Example using range

for i in range(len(my_list)):
    print i, my_list[i]
0 one
1 two
2 three
3 four

Note the use of item in the first, and i in the second. One denotes a string or some value, the other denotes an integer index.


Thank you for not just clear but fluid english. It really helps verbal learners who can't afford streaming. Class act.


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