Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Michael McGill
Michael McGill
2,085 Points

what does inclusive, exclusive mean in range/slicing?

I've googled for days and tried to read stack overflow websites and I still can't get a proper answer for beginners on what inclusive and exclusive means and why. I already know that in range, the starting index is always 0, but that's not the same for stop.

This is because: start is inclusive (what is it including?)

stop is exclusive. (what is it excluding?)

ie in slicing a list: food[eggs, milk]

food[0, 2]

If both start and stop were inclusive, then the slice would be food[0, 1]

If both start and stop were exclusive, then the slice would be food[1, 2]

but it's not.

1 Answer

inclusive and exclusive refer to the last value: inclusive and it is included; exclusive and it is excluded. For example

1 to 10 inclusive is 1,2,3,4,5,6,7,8,9,10

1 to 10 exclusive is 1,2,3,45,6,7,8,9