Quiz 2
Registry Synced

Programming in Python · Week 6 — Basic collections

1172 words
6 min read
2026-08-16

Reading compass

Now · Week map

Week 6 — basic collections

Quiz 2 scope: Weeks 1–8 per IITM May 2026 foundation courses. Source baseline: IITM BS admissions important-dates calendar · May 2026 cycle. Times on assessments are operational conventions — verify hall ticket.

Week map

Sequence types → indexing → slice → mutate list not tuple

Classify → Represent → Execute → Trap-check

  • Recognize: Ask: What does seq[-2] return?
  • Procedure: Last element is index -1. Slice [1:4] takes indices 1,2,3. Step 2 skips every other. Empty slice when start >= end with positive step.
  • Variations / traps: Watch for: IndexError on len(seq) or -len-1.

Formula chain (compressed)

list mutable vs tuple immutable → indexing/slicing → append vs +.
  1. List literal[a, b, c] — ordered, mutable sequence
  2. Tuple literal(a, b, c) — ordered, immutable
  3. Index / sliceL[i], L[a:b:c] — 0-based, stop exclusive
  4. appendL.append(x) — mutate in place
  5. ConcatenateL + M → new list — does not mutate L

Deep study

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](/viewer?path=1, 0], [1, 0) — 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]?

ChatGPT prep archive

Archived import for extra depth — complements the notes above, not official IITM material.

Core concepts

  • List []: ordered, mutable; tuple (): ordered, immutable.
  • Indexing: 0-based; negative indices from end; seq[i] one element.
  • Slicing: seq[start:end:step] copies subsequence; end exclusive.
  • Methods: append, pop; + concatenates sequences.

Notation & vocabulary

OperationListTuple
Mutateyesno
a[i]read/writeread only
Slicenew listnew tuple

Pattern families

Easy — Index and slice

Last element is index -1. Slice [1:4] takes indices 1,2,3. Step 2 skips every other. Empty slice when start >= end with positive step.

Medium — Mutate vs share

Assignment copies reference for lists—two names can point to same list. Slicing often creates new sequence. Tuple assignment unpacks length must match.

Hard — List algorithms trace

append grows end; pop removes indexed item. Track length inside loop. Slicing mid-loop uses updated list state.
Drill these on the pattern atlas — filter to week 6.

Traps

  • IndexError on len(seq) or -len-1.
  • Confusing slice copy with alias mutation.
  • Using tuple where list needed for append.
  • Step 0 in slice is invalid.

Retrieval prompts

  1. What does seq[-2] return?
  2. Can you append to a tuple?
  3. Difference between a = b for lists vs a = b[:]?

Practice loop

  1. Read Deep study (if present) or core concepts once.
  2. Recite the formula chain without looking.
  3. Open one easy pattern on the interactive atlas for week 6.
  4. Attempt without solutions; mark studied after an honest try.
  5. Say one trap aloud before closing the tab.
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.