Reputation: 534
the resource i am using to learn python has you perform modules within its own website. i think it was originally designed for students of this particular university so that is why i have tagged this with homework, even though it is not.
anyway:
they had me perform this task:
Define a function prod(L) which returns the product of the elements in a list L.
i got this function to work using this code:
def prod(L):
i = 0
answer = 1
x = len(L)
while i < x:
answer = answer * L[i]
i = i + 1
if i == x:
return answer
the very next module talks very briefly about For-In loops. and they pose the question:
Define the function prod(L) as before, but this time using the new kind of loop.
i have tried looking through other resources to understand how exactly to use this but i am not following anything. can anybody explain, preferably in plain english, how a for-in loop works?
for reference: here is EVERYTHING that they talked about regarding the for-in loop
Looping through lists It is very common (like in the previous exercise) to loop through every value in a list. Python allows a shortcut to perform this type of an operation, usually called a "for all" loop or a "for each" loop. Specifically, when L is a list, this code
for x in L: «loop body block»
does the following: first x is set to the first value in L and the body is executed; then x is set to the second value in L and the body is executed; this is continued for all items in L.
i just cant fully wrap my head around this. im not looking for answers as i am doing this for knowledge growth - but i feel like im falling behind on this ):
Upvotes: 2
Views: 2731
Reputation: 208475
Hopefully this simple example will help to clarify, the following two pieces of code do the same thing:
Using a while
loop:
L = ['a', 'b', 'c']
i = 0
while i < len(L):
print L[i]
i += 1
Using a for
loop:
L = ['a', 'b', 'c']
for c in L:
print c
If you want the index AND the element like you have with a while
loop, the pythonic way to do it is with enumerate:
L = ['a', 'b', 'c']
for index, element in enumerate(L): # returns (0,'a'),(1,'b'),(2,'c')
print index
print element
As you can see above, the for
loop lets you iterate directly over the contents of an iterable, as opposed to the while
loop method where you keep track of an index and access items with the index.
Upvotes: 6
Reputation: 2056
So, let's say I have a list of 10 numbers (integers, let's say).
A common task I might want to do would be to go through this list, and if the number is even, I add 10.
So, if I input: [1, 2, 3, 4, 5] I would get: [1, 12, 3, 14, 5]
One thing I could do is loop though, and for each index, check if the value at each index is even or odd.
But, what if I don't want to think about indices? I know I have to go through the entire list anyway, and I don't really need to know the index of the element for finding if the element is even or odd.
So, instead of:
lst = [1, 2, 3, 4, 5]
newLst=[]
i=0
while i < 10:
if (lst[i]%2==0): # if element i of lst is even
newLst+= [ lst[i]+10 ]
i+=1
I could just do:
for element in list:
if(element%2==0):
newLst+= [ element+10]
else: newLst+= [element]
By doing this, I don't have to worry about indices, and I know element
will be one item from the list.
Upvotes: 0
Reputation: 3215
In your first version, you defined an index variable i, then accessed L[i]. There's no other need for i, other than the if i == x return.
The "for x in sequence" idiom abstracts away the index variable. Instead of directly accessing L[i], you have a variable that will represent each item in L in a loop.
Upvotes: 0
Reputation: 798676
In the explanation given, L
is an iterable. This can be a sequence (e.g. list, tuple, string) or a generator (e.g. xrange()
). Each element of the iterable is bound to the name x
in turn, and the loop body is executed.
Upvotes: 1
Reputation: 715
A for loop will loop through all of the contents of an iterable (usually a list) and perform an action against each member object, and then break out of the loop when there are no more objects to loop through. Python standard docs explain this well:
http://docs.python.org/tutorial/controlflow.html#for-statements
so for exaple the code
for x in xrange(10):
print x
will print the numbers 0 through 9
Upvotes: 3