[Tutor] foreach loops

Luke Paireepinart rabidpoobear at gmail.com
Mon Sep 11 17:04:17 CEST 2006


Danny Yoo wrote:
>   
>> Does python have foreach loops?  I don't see any mention of them in the 
>> docs.  Am I going to have to use Perl (gasp!) if I want my beloved 
>> foreach loop?
>>     
>
> Can you show an example of a Perl foreach loop?  Perhaps someone can help 
> translate it into idiomatic Python.
>   
It seems to me that foreach is the same as the python 'for' loop....
#-- perl code
@myNames = ('Larry', 'Curly', 'Moe');
print "Who's on the list:\n"; foreach (@myNames) {
print $_ . "\n";
}

#--- python code:
names = ['Larry','Curly','Moe']
print "Who's on the list?\n"
for x in mynames:
    print x+'\n'
#----

Am I missing the point here?
-Luke
> Good luck!
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   



More information about the Tutor mailing list