This unit has now built four sequence types —
str, tuple, range, and list — each introduced separately, for a specific purpose. This short note steps back and puts them side by side: what operations do all four share, what distinguishes them, and — most practically — how does a programmer decide which one to reach for in a given situation? The single deciding factor, as this note will argue, is almost always mutability, with a handful of secondary considerations (element homogeneity, dictionary-key eligibility) settling the remaining cases.Learning objectives. By the end of this note you should be able to:
- list the operations common to all four sequence types (
str,tuple,range,list) and identify which types support concatenation and repetition and which do not; - state, and apply, the deciding rule for choosing between a tuple and a list in a given design situation;
- use the key
strmethods (split,find,replace,count,lower,rstrip) for common text-processing tasks; - build a list incrementally with
.appendinside aforloop — the standard idiom for producing a filtered or transformed list from an existing sequence.
In this lesson
- The common sequence interface
- String-specific methods
Before you start
The full lesson, with worked examples and exercise solutions, is for members.
Read the full lessonNew to SelfMadeQuant.com? See membership options or browse the Python for Quantitative Finance syllabus.