Re: [Tutor] foreach loops
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? Its called a for loop in Python... Or is there some extra magic in the Perl version that I'm missing? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] foreach loops
I was thinking more along the lines of this: A C++ for loop: This is exactly NOT a foreach loop, its a vanilla for loop. #include iostream using std::cout; int main() { for (int i = 0; i 10; i++) { cout i \n; } for i in range(10): print i Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] foreach loops
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. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] foreach loops
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@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] foreach loops
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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] foreach loops
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; } The same functionality can be provided using python for and the range() function, like: for i in range(0, 10): print i Though because of the way python works you don't need to use this type of loop anywhere near as often as in other languages. For example in java (and c++, but my c++ is so rusty I'm not going embarrass myself trying to write an example) you're constantly doing things like looping over an array: public void main(String[] args) { int[] foo; /* then later after foo has been initialized to whatever: */ for (int i=0; ifoo.length; i++) { System.out.println(foo[i]); } } The equivalent code in python is much cleaner, after foo has been initialized to whatever: for each in foo: print each Hope this helps, Jordan Greenberg ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor