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 route | How to reject it |
|---|---|
| Count the first item as position one | Label 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 beginning | Count backwards from the end. Minus one names the final item of a nonempty sequence. |
| Clip an excessive direct index or return None | Clipping applies to slice boundaries. A missing single sequence position raises IndexError. |
| Turn omitted forward bounds into zero and get an empty slice | For forward movement, omissions span from the beginning to the end. |
| Use a zero step for an empty result, or expect reversal to mutate the source | Zero cannot move and raises ValueError. A negative step creates a reversed result while leaving the source unchanged. |
| Require equal lengths for every slice assignment | A simple slice may grow or shrink a list. Only extended assignment with a non-unit step requires an exact replacement count. |
Try it
Classify the boundaries of the basic slice text[2:5]. Which statements are true? Select all that apply.
- The element at index 2 is included.
- The element at index 2 is excluded.
- The element at index 5 is excluded.
- The element at index 5 is included.
Classify a list slice assigned to a new name. Which statements describe the resulting copy? Select all that apply.
- The outer list is a new object.
- The slice is the very same outer list.
- Selected mutable elements may still be shared.
- Every selected object is recursively copied.
Given items = [1, 2, 3, 4], what happens when items[::2] = [9] is executed?
- It produces [9, 2, 4].
- It accepts any replacement length.
- It raises TypeError because stepped slices are read-only.
- It raises ValueError because two positions need two values.