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

Python Python Sequences Sequence Operations Slices

Natalie Heyde
Natalie Heyde
1,736 Points

Why are there 4 elements in the slices.py example?

Hi,

I thought slices could only have 3 elements in them. I was wondering why in the slices.py file, there is a slice with 4 elements that looks like: nums_partial = nums[0::2] ?

Unless I'm interpreting it incorrectly, and it's more like nums_partial = nums[0:_:2], meaning a start of 0, no stop, and step of 2?

Is : a placeholder, or a separator for these values?

1 Answer

Steven Parker
Steven Parker
229,732 Points

The colon (:) is the separator between the arguments (up to 3) in a slice.

You are correct that the expression nums[0::2] represents a start of 0, no stop, and a step of 2.

There is no limit on the number of elements a slice can contain. With these arguments, the resulting slice (nums_partial) will contain half as many elements as the original (nums) has.