Python Study Notes

Lecture 5, Part 1: Tuples, Lists, and Core Operations

A structured review of ordered collections, from fixed tuple records to editable lists and their first essential operations.

A fixed enclosed sequence beside an expandable sequence receiving a new item

MIT 6.0001 learning journal

Introduction to Computer Science and Programming in Python

5 / 12

This post is part of my lecture-by-lecture review. Each entry turns the lecture into a set of mental models, annotated examples, and original practice questions I can revisit later.

Now studying Lecture 5 - Part 1 focuses on tuples, list fundamentals, and introductory operations.

The programs from earlier lectures mainly worked with individual values. Lecture 5 introduces compound data types: objects that group multiple values into one ordered structure. That change sounds small, but it lets a program model coordinates, course records, collections of scores, and much more.

This first part focuses on tuples and the foundations of lists. I stop after a small group of list operations so that the distinction between creating a new collection and changing an existing one remains clear. Part 2 will continue with removal, conversion, sorting, aliasing, cloning, and the side effects of mutation.

01

Tuple: a fixed record

A tuple is ordered and immutable. Its positions can represent fields that belong together and should not be replaced in place.

Core question: Should this collection stay fixed after it is created?

02

List: an editable collection

A list is ordered and mutable. Its elements can be replaced, and operations can add more items to the same list object.

Core question: Will this collection need to change as the program runs?

1. Compound data types group related values

An integer, float, or Boolean usually represents one value. A tuple or list can hold an ordered sequence of values under one name. Both support familiar sequence tools: len(), zero-based indexing, negative indexing, slicing, and iteration with a for loop.

coordinates = (53.5461, -113.4938)
topics = ["tuples", "lists", "iteration"]

print(coordinates[0])  # 53.5461
print(topics[-1])      # iteration
print(len(topics))     # 3

Order matters. Position 0 is the first element, position 1 is the second, and so on. The major difference is not how the values are accessed; it is whether the collection can be changed after creation.

2. Tuples are ordered and immutable

A tuple is usually written as comma-separated values inside parentheses. It can contain different data types and can even contain other tuples. Once created, however, its elements cannot be reassigned.

student = ("Lance", "Computer Science", 2026)

print(student[0])   # Lance
print(student[1:])  # ('Computer Science', 2026)

# student[2] = 2027
# TypeError: tuple elements cannot be reassigned

Immutability makes a tuple useful when the positions form one stable record. A coordinate pair, an RGB colour, or a function's multi-part result is often easier to understand when its structure is not expected to grow or shrink.

3. Tuple operations create new tuples

Tuples support indexing, slicing, length checks, membership tests, and concatenation. Because a tuple is immutable, slicing or joining does not edit the original tuple; the operation produces another tuple.

core = ("tuples", "lists")
expanded = core + ("iteration",)

print(core)              # ('tuples', 'lists')
print(expanded)          # ('tuples', 'lists', 'iteration')
print("lists" in core)   # True
print(expanded[:2])      # ('tuples', 'lists')

This is the same broad sequence model used for strings in Lecture 3. The syntax changes, but the ideas of position, slice boundaries, and traversal carry forward.

4. Packing and unpacking connect names to positions

Python can pack several values into one tuple and unpack that tuple into separate variables. The number of names on the left must match the number of values being unpacked.

profile = ("Lance", "Edmonton", "Computer Science")
name, city, program = profile

print(name)     # Lance
print(city)     # Edmonton
print(program)  # Computer Science

Tuple unpacking also makes swapping two values concise. Python evaluates the values on the right, packs them together, and then assigns them to the names on the left.

left = "A"
right = "B"

left, right = right, left

print(left, right)  # B A

5. Functions can return multiple values as a tuple

Lecture 4 established that a function's returned value can be reused by its caller. A tuple allows one return statement to carry several related results. The caller can keep the tuple or unpack it immediately.

def quotient_and_remainder(dividend, divisor):
    return dividend // divisor, dividend % divisor

quotient, remainder = quotient_and_remainder(17, 5)

print(quotient)   # 3
print(remainder)  # 2

The expression after return creates the tuple (3, 2). Unpacking then gives each position a meaningful name. This is especially helpful when both values describe one computation.

6. Tuples can be traversed directly

A for loop visits tuple elements in order. Direct iteration is the clearest option when the program needs the values but not their index positions.

scores = (82, 91, 76, 88)
total = 0

for score in scores:
    total += score

average = total / len(scores)
print(average)  # 84.25

The tuple itself remains unchanged. The loop only reads each element and updates a separate accumulator named total.

7. Lists are ordered and mutable

A list uses square brackets. Like a tuple, it is ordered, indexed, sliceable, and iterable. Lists commonly hold values of the same kind, although Python permits mixed types and nested lists.

topics = ["tuples", "lists", "functions"]
grid = [[1, 2], [3, 4]]

print(topics[1])  # lists
print(grid[0])    # [1, 2]
print(grid[1][0]) # 3

In a nested list, the first index selects an inner list and the second selects an element inside it. Reading grid[1][0] from left to right means: take the second inner list, then take its first value.

8. List elements can be replaced in place

List mutability means an assignment can replace an element at a particular index. The variable still refers to the same list, but that list now contains a different value.

topics = ["tuples", "arrays", "iteration"]

topics[1] = "lists"

print(topics)  # ['tuples', 'lists', 'iteration']

This is the first important contrast with tuples. The equivalent tuple assignment would raise a TypeError. Mutability is convenient, but it also creates questions about shared references and side effects. Those questions belong to Part 2.

9. Three ways to add list elements

This part ends with a focused comparison. append() adds one item to the end of the existing list. extend() adds each item from another iterable to the existing list. The + operator combines two lists into a new list.

topics = ["tuples", "lists"]

combined = topics + ["functions"]  # creates a new list
topics.append("iteration")         # changes topics
topics.extend(["indexing", "slicing"])  # changes topics

print(combined)
# ['tuples', 'lists', 'functions']

print(topics)
# ['tuples', 'lists', 'iteration', 'indexing', 'slicing']
list.append(item)Adds one item to the end and mutates the list.
list.extend(items)Adds the elements from another iterable and mutates the list.
left + rightCreates a new combined list while leaving both input lists unchanged.

A method call such as topics.append(...) uses dot notation: Python performs an operation associated with the list object. These mutating methods return None, so the useful result is the changed list itself, not a separate returned collection.

Practice lab

MIT's in-class questions begin by testing the singleton-tuple rule, index-based list changes, and the difference between list operations. I used those themes to create these original self-checks. Try tracing each example before opening the answer.

01 Tuple or string?
first = ("cloudy")
second = ("cold",)

print(type(first).__name__)
print(type(second).__name__)
print(len(second))

Answer: Python displays str, tuple, and 1. Parentheses alone do not create a one-item tuple; the trailing comma does.

02 Unpack and swap
point = (4, 9)
x, y = point
x, y = y, x

print(x, y)

Answer: The output is 9 4. The first assignment unpacks the tuple, and the second packs the values in reverse order before unpacking them again.

03 Return two related results
def split_seconds(total_seconds):
    return total_seconds // 60, total_seconds % 60

minutes, seconds = split_seconds(185)
print(minutes, seconds)

Answer: The output is 3 5. The function returns the tuple (3, 5), which the caller unpacks into two names.

04 Trace index-based list changes
words = ["life", "answer", 42, 0]
words[0] = "universe"
words[1] = "everything"
words[-1] = words[2]

print(words)

Answer: The list becomes ["universe", "everything", 42, 42]. Each assignment replaces an element in the existing list; -1 selects the final position.

05 Compare +, append(), and extend()
left = [1, 2]
right = left + [3]
left.append(4)
left.extend([5, 6])

print(left)
print(right)

Answer: left is [1, 2, 4, 5, 6], while right remains [1, 2, 3]. Concatenation created a separate list before the two mutating method calls changed left.

Key takeaways

  • Tuples and lists are compound data types that store ordered sequences of values.
  • Both support indexing, slicing, len(), membership tests, and direct iteration.
  • A tuple is immutable, and a one-item tuple requires a trailing comma.
  • Tuple packing and unpacking make swaps and multi-value function returns concise.
  • A list is mutable, so an element can be replaced through index assignment.
  • append() and extend() mutate a list, while + creates a new list.
  • The choice between a tuple and a list should communicate whether the collection is meant to stay fixed or change.
  • Aliasing, cloning, broader list operations, and mutation side effects are intentionally reserved for Part 2.

Course material and further practice

Continue the conversation

Have a correction, a better example, or a technical topic to discuss?

Contact Lance