Reputation: 75
I'm trying to create a for
loop and I ran into problems. I don't understand how these loops work, and I think the problem is because I'm using the for
syntax incorrectly.
From what I understand, a for
loop should look like
for w in words:
print(w, len(w))
But how exactly does it work?
To try to be specific: What does the w
mean? How can I know what to write between for
and in
, and after in
? What exactly happens when the code runs?
For a more technical breakdown of how for
loops are implemented, see How does a Python for loop with iterable work?.
Upvotes: 4
Views: 7433
Reputation: 12316
A for
loop takes each item in an iterable and assigns that value to a variable like w
or number
, each time through the loop. The code inside the loop is repeated with each of those values being re-assigned to that variable, until the loop runs out of items.
Note that the name used doesn't affect what values are assigned each time through the loop. Code like for letter in myvar:
doesn't force the program to choose letters. The name letter
just gets the next item from myvar
each time through the loop. What the "next item" is, depends entirely on what myvar
is.
As a metaphor, imagine that you have a shopping cart full of items, and the cashier is looping through them one at a time:
for eachitem in mybasket:
# add item to total
# go to next item.
If mybasket
were actually a bag of apples, then eachitem
that is in mybasket
would be an individual apple; but if mybasket
is actually a shopping cart, then the entire bag could itself meaningfully be a single "item".
Upvotes: 5
Reputation: 891
A for
loop works on an iterable: i.e., an object that represents an ordered collection of other objects (it doesn't have to actually store them; but most kinds of iterable, called sequences, do). A string is an iterable:
>>> for c in "this is iterable":
... print(c, end=" ")
...
t h i s i s i t e r a b l e
However, a number is not:
>>> for x in 3:
... print("this is not")
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
The built-in range
function allows an easy way to iterate over a range of numbers:
>>> for x in range(3):
... print(x)
...
0
1
2
In 2.x, range
simply creates a list with those integer values; in 3.x, it makes a special kind of object that calculates the numbers on demand when the for
loop asks for them.
Upvotes: 3
Reputation: 82470
In simple terms, Python loops over an iterable object, such as a string, list or tuple. For a list, it will loop through the list elements:
>>> numbers = [1, 2, 3, 4, 5]
>>> for n in numbers: print(n)
1
2
3
4
5
For a string, it will loop through individual characters (technically, Unicode code points):
>>> my_happy_string = "cheese"
>>> for c in my_happy_string: print(c)
c
h
e
e
s
e
Here is another example with a list of words:
>>> list_of_words = ["hello", "cat", "world", "mouse"]
>>> for word in list_of_words: print(word)
hello
cat
world
mouse
If you want a for
loop that would start at 0
and end at 10
, you can use the built-in range
function:
>>> for i in range(0, 10): print(i)
0
1
2
3
4
5
6
7
8
9
Upvotes: 1
Reputation: 16007
What you have is more like a foreach
loop.
Maybe something like this:
input = raw_input("> ") #separate with spaces
sum = 0
numbers = [int(n) for n in input.split()]
for number in numbers:
sum = sum + number
print sum
Upvotes: -1