QuestenaPractice that shows what to review next
Topic lesson

Slicing and indexing: trace positions and boundaries

About 5 min

Questions stay in the language in which they were published.

These questions ask you to locate one sequence item, calculate a slice, or predict how slice assignment changes a list. Draw the positions first, then decide whether the expression requests one position or a range.

Separate one position from a range

Indexing is zero-based from the left and negative from the right. In word = "planet"; word[0], the position is the first character, while word[-1] is the final character. A single position must exist; an excessive direct index raises IndexError rather than clipping or returning None.

A slice uses start:stop:step. For a positive step, start is included, stop is excluded, and omitted bounds mean the beginning and end. In word = "planet"; word[1:4], positions one through three are selected. Bounds beyond the endpoints are clipped. The step controls movement and cannot be zero. A negative step reverses direction, so word[::-1] builds a reversed string value without changing word.

Slicing produces a result of the same sequence family. A string slice is another immutable string. A list slice creates a new outer list but retains references to the selected element objects, so nested mutable elements may still be shared. Keep reading and assignment separate: part = items[:] makes a shallow outer copy, while items[1:2] = [7, 8] changes items in place and may change its length. If a step other than one selects fixed positions, the replacement must provide exactly that many values.

Reject these tempting routes

Tempting routeHow to reject it
Count the first item as position oneLabel positions from zero. The count of items and the final nonnegative index differ by one.
Reject every negative index or make minus one wrap to the beginningCount backwards from the end. Minus one names the final item of a nonempty sequence.
Clip an excessive direct index or return NoneClipping applies to slice boundaries. A missing single sequence position raises IndexError.
Turn omitted forward bounds into zero and get an empty sliceFor forward movement, omissions span from the beginning to the end.
Use a zero step for an empty result, or expect reversal to mutate the sourceZero cannot move and raises ValueError. A negative step creates a reversed result while leaving the source unchanged.
Require equal lengths for every slice assignmentA simple slice may grow or shrink a list. Only extended assignment with a non-unit step requires an exact replacement count.

Try it

Question 100

Classify the boundaries of the basic slice text[2:5]. Which statements are true? Select all that apply.

  1. The element at index 2 is included.
  2. The element at index 2 is excluded.
  3. The element at index 5 is excluded.
  4. The element at index 5 is included.
Question 105

Classify a list slice assigned to a new name. Which statements describe the resulting copy? Select all that apply.

  1. The outer list is a new object.
  2. The slice is the very same outer list.
  3. Selected mutable elements may still be shared.
  4. Every selected object is recursively copied.
Question 108

Given items = [1, 2, 3, 4], what happens when items[::2] = [9] is executed?

  1. It produces [9, 2, 4].
  2. It accepts any replacement length.
  3. It raises TypeError because stepped slices are read-only.
  4. It raises ValueError because two positions need two values.
Start drillPractice this topic
Slicing and indexing: trace positions and boundaries · Questena