Dav nailed down perfectly why the syntax you wrote doesn't work.
Here are the syntaxes that do work for what you're probably trying to do:
If you want all 4 x 4 combinations for x and y, you want 2 nested loops:
for x in range(4):
for y in range(4):
print x, y
Or if you really want to use one loop:
import itertools
for (x, y) in itertools.product(range(4), range(4)):
print x, y
itertools.product()
generates all possible combinations:
This is less readable than 2 loops in this simple case, but the itertools module has many other powerful functions and is worth knowing...
If you want x
and y
to advance in parallel over two sequences (aka "lock-step" iteration):
for (x, y) in zip(range(4), range(4)):
print x, y
# `zip(range(4), range(4))` is silly since you get x == y;
# would be useful for different sequences, e.g.
# zip(range(4), 'abcd')
[Background: The name zip
comes from Haskell; think about how a Zipper takes one tooth from here and one from there:
zip()
cuts off to the length of the shortest sequence; the itertools module has other variants...]