More with 'for'


#1




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


Replace this line with your code.


#2

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.


#3

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


#4

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


#5

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"


#6

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
'''
one
two
three
four
'''

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.


#7

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


#8

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