Agenda

  • Assignment 3.3 (started yesterday) is due on 9/10
  • Assignment 3.4 starting today is due on 9/11
  • PE 1 Module 3 Quiz is due 9/11
  • PE1 Module 3 Test is due 9/12
  • Unit 3 Quiz (on paper) will be administered in class on Friday
    • Topics assessed: Lists, Tuples, Dictionaries

Room Schedules

Note for 3rd Block:

We will NOT be going to Duke Centennial Hall on Tuesdays and Thursdays. It was determined to be too far of a walk. So we'll stay in Belk on those days.

Reminder for 4th Block:

Tomorrow (Thursday) we'll meet outside Cone in the courtyard to walk to class (in Smith Hall) together

Lesson 3.4 – Tuples

What we’ll learn today

  • What a tuple is
  • How tuples are different from lists
  • Tuple packing & unpacking
  • Practical uses for tuples

Quick Review

  • How do you loop through a list?
  • What does the in keyword do?

Tuples Basics

  • A tuple is like a list, but immutable (cannot change).
point = (3, 4)
print(point[0])   # 3
print(point[1])   # 4

Key Difference

  • Lists: mutable (can change)
  • Tuples: immutable (cannot change)
nums = (1, 2, 3)
# nums[0] = 5   ❌ Error! Cannot change a tuple

Packing and Unpacking

  • Packing: putting values into a tuple
coords = (10, 20)
  • Unpacking: splitting into variables
x, y = coords
print(x)   # 10
print(y)   # 20

Class Question

  • Why might it be useful to keep some data unchangeable?

Tuple Tricks

  • Tuples can be used to return multiple values:
def min_and_max(nums):
    return (min(nums), max(nums))

low, high = min_and_max([4, 7, 1, 9])
print(low, high)   # 1 9

Mini Practice

  1. Create a tuple with your name and age.
  2. Unpack it into two variables.
  3. Print them out.

Mini Challenge

  • Write a program that:
    1. Creates a tuple (5, 10, 15)
    2. Unpacks the values into a, b, c
    3. Prints a + c