DEV Community

Discussion on: Dead Simple Python: Iteration Power Tools

Collapse
 
turvey profile image
Marc Turvey

Such a great series, just what I need to get started with Python again! I am slightly confused around the enumeration examples though, the first snippet and second snippet read the same other than the output text, should the first snippet read as below with a 0 rather than a 1 on the enumeration?

for index, value in enumerate(range(10,101,10), 0):
    print(f'Element {index} is has the value {value}.')

Cheers
Marc

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

You could do that. I deliberate used a 1 because I wanted to use 1-based indexing in my output, instead of 0-based. I personally liked it better, that's all.

If you want to start from 0, as in your code, you can actually omit the second argument altogether, and just use enumerate(range(10,101,10)).

Collapse
 
turvey profile image
Marc Turvey

That makes sense, however in order to get the result provided in the post for your first snippet, you would have needed to have put a 0 or omitted the second argument, which is what I found confusing as the second snippet then shows the correct results for using a 1.

Cheers
Marc

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

Oh! Derp, I see it now. Sorry, typo! Fixed.

Thread Thread
 
turvey profile image
Marc Turvey

No worries! Just glad it wasn’t me not getting it.

Keep up the good work, this is a great series!

Cheers
Marc