UcenlyOpen in the app

Programming Essentials · BE5B33PRG · Topic 3 of 9

Strings, lists and tuples

All are sequences, but their mutation rules differ.

Open the interactive lesson

Key ideas

Index and slice

s = "python"; s[0] is "p", s[-1] is "n", and s[1:4] is "yth". The stop index is excluded.

Mutation

list elements can be assigned: a = [1, 2]; a[0] = 3. Strings and tuples cannot be changed in place.

Traversal

for item in items visits values; enumerate(items) provides index and value. len(items) counts elements.

Aliasing

b = a makes two names refer to one list; b.append(4) also changes what a sees. a[:] makes a shallow outer-list copy.

In one line: Ask whether an operation creates a new object or changes the existing one.

Sources

Written by Ucenly. Based on the BE5B33PRG course materials: