scipy-lectures / scientific-python-lectures

Tutorial material on the scientific Python ecosystem
https://lectures.scientific-python.org
Other
3.11k stars 1.19k forks source link

Addition in list indexing related to negative step #782

Open linnabraham opened 4 months ago

linnabraham commented 4 months ago

In https://lectures.scientific-python.org/intro/language/basic_types.html#lists it is mentioned that all slicing parameters are optional. And a few examples are shown to demonstrate what values are implicitly set when you skip these.

However the examples miss a case with negative step value. With a negative step value, the start is implicitly set to -1. Which differs from positive step value cases, where if the start is skipped, the start index is implicitly set to 0.

Although negative step is demonstrated later on, it is just with regarding to reversing a list. This aspect is not made clear there. Maybe this can also be a warning or something.?

stefanv commented 4 months ago

Thanks for reporting; it's about time we give this material another review.

linnabraham commented 4 months ago

How does one contribute to the material? Is it enough to report any such issue or would it be better to make a pull request?

rossbar commented 4 months ago

FWIW I think the case you describe is sufficiently illustrated in the third example under the Lists heading. However, please feel free to open PRs with suggestions!

linnabraham commented 4 months ago

@rossbar Thanks for letting me know that PRs are welcome.

If you were pointing to this colors[-1], that is not the case I meant. Here you are explicitly indexing using a negative index and hence its clear. But suppose you use the start:stop:step with colors[::-1] or colors[::-2] the start and end indices are omitted but implicitly the start is set to -1. That is colors[-1::-1] or colors[-1::-2] would give identical results. This thing that happens under the hood is not clear to the reader, in my opinion, when this example is listed further down as an example of list reversing.