Quiz 2

648 words
3 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Programming in Python · Week 6 — Lists and tuples Deep study for Quiz 2 week 6. Lists are mutable sequences; tuples are immutable — choose based on whether data should change.

Programming in Python · Week 6 — Lists and tuples

Deep study for Quiz 2 week 6. Lists are mutable sequences; tuples are immutable — choose based on whether data should change.

Week map

List creation → indexing/slicing → mutation methods → traversal → tuple immutability → list vs tuple choice.

List notation

  • lst = [1, 2, 3] → ordered mutable sequence → elements accessible by index.
  • lst[i] → element at index i → 0-based → lst[0] is first.
  • lst[-1] → last element → negative index counts from end.
  • lst[a:b] → slice from a inclusive to b exclusive → lst[1:3] gives two elements.
  • len(lst) → number of elements.
Mini-example:
python
colors = ["red", "green", "blue"]
colors[1] = "yellow"   # mutable
colors.append("black") # [red, yellow, blue, black]

Common list methods

  • .append(x) → add one item at end.
  • .insert(i, x) → insert at index i, shift right.
  • .pop() → remove and return last; .pop(i) at index.
  • .remove(x) → remove first occurrence of value x.
  • lst + other → concatenate → new list.
  • lst * n → repeat list n times.
Trap: append adds one element; extend adds each element of iterable.

Tuple notation

  • t = (1, 2, 3) → ordered immutable sequence → cannot reassign elements.
  • t = (42,) → single-element tuple needs trailing comma.
  • () → empty tuple.
  • Tuples useful for fixed records: (name, score), coordinates (x, y).
Mini-example:
python
point = (3, 4)
x, y = point          # unpacking
# point[0] = 5       # TypeError — immutable

List vs tuple

FeatureListTuple
MutabilityYesNo
Syntax[ ]( )
Use caseGrowing/changing dataFixed bundles

Pattern families

Easy — Index and slice

Access first, last, middle. Slice subsequence. Predict result of lst[::2] (every second element).

Medium — Mutate and traverse

Append, insert, pop in trace problems. Loop with for x in lst or index for i in range(len(lst)). Build new list by comprehension or loop.

Hard — Nested lists and aliasing

List of lists — inner lists are references. a = b aliases same list; a = b[:] shallow copy. Modify inner list through one alias affects other.

Worked mini-examples

Example 1 — Slice.
python
nums = [10, 20, 30, 40, 50]
nums[1:4]    # [20, 30, 40]
nums[-2:]     # [40, 50]
Example 2 — append vs insert.
python
a = [1, 2]
a.append([3])     # [1, 2, [3]]
b = [1, 2]
b.insert(1, 99)   # [1, 99, 2]
Example 3 — Traverse and transform.
python
vals = [1, 2, 3]
doubled = []
for v in vals:
    doubled.append(v * 2)
# [2, 4, 6]
Example 4 — Tuple unpack.
python
pair = ("Alice", 92)
name, score = pair
Example 5 — Aliasing trap.
python
row = [0, 0]
grid = [row, row]
grid[0][0] = 1
# grid is [1, 0], [1, 0](/courses/deep/notes/1%2C%200%5D%2C%20%5B1%2C%200) — both rows share row

Traps

  • t = (5) is int 5, not tuple — need (5,).
  • append([x]) nests list; extend([x]) adds element.
  • Slice copy is shallow — inner mutable objects still shared.
  • Modifying list while iterating forward can skip elements.
  • lst.sort() sorts in place; sorted(lst) returns new list.

Diagnostic (try yourself)

  1. What is ["a", "b", "c"][1]? What is [-1]?
  2. After x = [1, 2]; x.append(3); x.insert(0, 0), what is x?
  3. Can you change t[0] if t = (10, 20)? Why or why not?
  4. What does a = [1, 2] + [3] produce? How is it different from a.append(3) starting from [1, 2]?
  5. row = [0, 0]; m = [row, row]; m[1][1] = 9. What is m[0]?
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.