DEV Community

Cover image for Dead Simple Python: Iteration Power Tools

Dead Simple Python: Iteration Power Tools

Jason C. McDonald on March 02, 2019

Like the articles? Buy the book! Dead Simple Python by Jason C. McDonald is available from No Starch Press. The previous section more or less e...
Collapse
 
ardunster profile image
Anna R Dunster

I find it fairly easy to intuit how range works, because mostly I've used it for range(len(something)) (alternatively range(1,len(something))) in which case it grabs every index of something. Like, if len(something) == 10, then the last item in something has an index of 9, so it makes sense that range wouldn't want to try to grab an index 10.

Sometimes this throws me off trying to use actual numbers in range(), though XD

For some reason I often have trouble confusing the syntax of slices and ranges, though. Less as I practice more, but it's not uncommon for my first entry of a statement to have : where it should have ,, or vice versa. Oops.

(Also, stepping by -1 is fun ;) )

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

Collapse
 
connorjcantrell profile image
connorjcantrell

Great article! Just a heads up, I found a typo when you were talking about reversed()

Collapse
 
codemouse92 profile image
Jason C. McDonald

Awesome! What was the typo? Do you remember?

Collapse
 
zeroknight profile image
Alex George

He may be referring to the end of the first paragraph in the section on reversed(): you wrote "reserved" instead. I was going to point out the same thing.

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

Good catch! Thanks.

Collapse
 
corbee profile image
Illia Pyshniak

There are typos in some print's — you forgot to close ) brackets.

Thanks for your huge work, it helps me a lot!

Collapse
 
codemouse92 profile image
Jason C. McDonald

Oh, thanks for catching that!