Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
The list is days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]. Write the slice, with the right positions, for each of these.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. A slice days[start:stop] starts at start and stops before stop. The positions are 0 for Mon, 1 for Tue, 2 for Wed, 3 for Thu, 4 for Fri, 5 for Sat and 6 for Sun.
days[:3] gives ['Mon', 'Tue', 'Wed'].days[-2:] gives ['Sat', 'Sun'].days[2:5] gives ['Wed', 'Thu', 'Fri'].days[1:-1] gives ['Tue', 'Wed', 'Thu', 'Fri', 'Sat'].Check. Run the four slices. The number of items in a slice days[a:b] is b - a: for the third one, 5 - 2 = 3.
Answer: days[:3], days[-2:], days[2:5] and days[1:-1].
Log in to join the discussion.
Loading…