Python Forum

Full Version: Upper-Bound Exclusive Meaning
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm not entirely sure if this topic should be posted in "General Coding Help," But I hope its fine.

What does Upper-Bound Exclusive mean?

Whilst taking lessons on Python, my Instructor said mid-sentence that, "...since the slicing syntax is upper-bound exclusive..."
Taken aback, I began researching what Upper-bound exclusive means, but to no avail.

If there are more important threads, I suggest you go there. This is not a enhancingly important thread, but I strive to understand Programming and what code does, therefore I'd appreciate it if someone could give me a explanation.

Thanks,
Johnny
Say you have a slice from a to b (a:b). If you think of 0 being the index of the first item, 1 being the index of the second item, and so on, then a:b gets items with the indexes a, a + 1, a + 2, ..., b - 2, and b - 1. Note that b is not in that list. It is excluded, and it is the upper bound, so this is called upper bound exclusive.

I prefer to think of the indexes as being between the items, rather than on the items:

Output:
lunch = ['Spam', 'spam', 'spam', 'eggs', 'spam'] ^ ^ ^ ^ ^ ^ | | | | | | 0 1 2 3 4 5 -5 -4 -3 -2 -1
So if a is 1 and b is 4, you get the three items between 1 and 4 in the above diagram.