[Tutor] foreach loops
Luke Paireepinart
rabidpoobear at gmail.com
Mon Sep 11 17:39:05 CEST 2006
Christopher Spears wrote:
> Hmmm...Perl is probably a bad example. My apologies.
> I was thinking more along the lines of this:
>
> A C++ for loop:
>
> #include <iostream>
>
> using std::cout;
>
> int main() {
>
> for (int i = 0; i < 10; i++) {
> cout << i << "\n";
> }
>
> return 0;
> }
>
>
for i in range(10):
print i+'\n'
that does the same thing as
a = [0,1,2,3,4,5,6,7,8,9]
for i in a:
print i+'\n'
or
a = range(10)
for i in a:
print i+'\n'
Python's 'for' loop is not really meant as an iterator over a list of
numbers
so this feature isn't built into the loop itself, you have to use the
range function,
which just generates a list of numbers to iterate over.
Perhaps you should read an introductory Python tutorial.
Any one of them should cover the types of questions that people from
other languages have about Python.
It sounds like you know how to program already, so the 'python for
non-programmers' type of tutorial
may not be best-suited to you, but just look around.
If you have any more questions I'd be happy to answer them,
as would the rest of the list, I'm sure.
HTH,
-Luke
>
>
More information about the Tutor
mailing list