bslatkin / effectivepython

Effective Python: Second Edition — Source Code and Errata for the Book
https://effectivepython.com
2.24k stars 718 forks source link

item 6: odds and evens are opposites #38

Closed bolry closed 8 years ago

bolry commented 8 years ago

item 6, page 13, technicality, odds = a[::2] actually only gives the even indexes (it starts with index 0 = even) and should therefor be evens = a[::2].

The same for evens = a[1::2] should be odds = a[1::2] since with start on the odd index 1.

Kind regards, Bo Rydberg

bslatkin commented 8 years ago

It's the difference between indexes (starting at zero) and ordinals (starting at one).

>>> a = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
>>> odds = a[::2]
>>> evens = a[1::2]
>>> print(odds)
['red', 'yellow', 'blue']
>>> print(evens)
['orange', 'green', 'purple']

odds = a[::2] gives you the first, third, and fifth items from the list

bslatkin commented 8 years ago

But thanks for the report!