Is the Python for statement the same as for each in other languages?

J

jmDesktop

Thank you. It looks like it is, but I wanted to make sure I
understood. Also, I didn't see a "regular" for loop construct either
(i=0;i<=10;i++), etc. I'm still new at it, but is there one of those?
 
D

Diez B. Roggisch

jmDesktop said:
Thank you. It looks like it is, but I wanted to make sure I
understood. Also, I didn't see a "regular" for loop construct either
(i=0;i<=10;i++), etc. I'm still new at it, but is there one of those?

Yes, it's foreach. And for your usecase, use

for i in xrange(11):
...


Diez
 
B

bruno.desthuilliers

Yes, it's foreach. And for your usecase, use

for i in xrange(11):
...

Or if you want to both iterate over an iterable and have the 'loop
index', use enumerate:

for index, item in enumerate('this is a test'):
print index, ' : ', item
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top