Study guide: Build Programs with Text and Lists
For text="planet" and values=[4,8,3], find text[1:4],values[1] and len(values). Explain the excluded slice endpoint.
Ideas to explain in your own words
- Python sequences use zero-based positions: the first character is index 0. An index selects one character, while a value such as a word contains several. Our cases use nonempty ASCII text and valid indices. Drawing numbered boxes helps trace the selection without installing software.
- A Python slice start:stop includes the start position and excludes the stop position. The slice is a new sequence containing the selected segment. These cases use positive positions only. Counting indices and marking the excluded endpoint prevents a common off-by-one error.
- A list stores an ordered collection. Python lists use zero-based indexing, just like strings. Selecting one item does not add the entire list or sort it. The given order is part of the data, so the learner should preserve it while finding a requested position.
- The Python len function reports how many items a sequence contains. For a list, each element is one item, even when an element is itself a list. Counting outer items differs from adding their numeric values or counting all nested contents. These supplied expressions make the nesting visible.
Useful vocabulary
- index
- A position number in a sequence.
- slice
- A selected segment with a stated start and stop.
- list
- An ordered collection of items.
Watch your reasoning
- Index 1 selects the second character, not the first.
- Do not include the stop-position character.
- The smallest value is not necessarily the first list item.
- Do not replace list length with the sum of its values.
A worked review example
For "boat", label 0:b, 1:o, 2:a, 3:t; index 0 selects "b". For the distinct text "peach", label 0:p, 1:e, 2:a, 3:c, 4:h; index 2 selects "a". Read the original sequence without sorting and distinguish an index from its character value. Both supplied indices exist. An out-of-range request would need a separately specified error response rather than an invented character. The list [2,5,9] has three outer elements, so its length is 3, rather than their sum 16. The distinct nested list [[8],[4,6,2],[1,5]] also has three outer elements: the first, second and third inner lists. The six numbers inside those lists do not change the outer length to six. Counting outer elements differs from recursive counting. An empty collection has no outer elements; identify which collection is being measured.
Make a practice plan
- Make a four-row summary, one row for each lesson, with its computing idea and one worked decision.
- Explain one tempting mistake using the actual supplied example.
- Complete the new unit review before consulting its explanations.
- Try the transfer task on paper and compare your evidence against the model response.