1 of 18

CSE 160 Section 7

Tuples & Mutability!

CSE 160: Tuples & Mutability

2 of 18

Logistics

  • Written check-in 7 due Friday Feb 20!
  • Programming practice 7 due Wed, Feb 25!
  • HW4 due Thu Feb 26
    • Submitting on Gradescope
    • Wait for autograder!

CSE 160: Tuples & Mutability

3 of 18

Lecture Review: Tuples

CSE 160: Tuples & Mutability

4 of 18

Tuples

  • Tuple is a collection which is ordered and unchangeable.
  • Lists and tuples are similar, but have different properties
  • The table at the right shows what kind of things you can do with a tuple, but not a list.
  • Let the data structure be called name. A ✅ means you can do it, a 🚫 means it won’t work

Description

Example

list

tuple

indexing

name[i]

negative indexing

name[-i]

slicing

name[i:j]

checking if item exists

item in name

looping

for item in name

length

len(name)

changing items

name[i] = item

🚫

appending items

name.append(item)

🚫

use as dict keys

dict(name:val)

🚫

CSE 160: Tuples & Mutability

5 of 18

Making a Tuple

These are all ways to make tuples:

Note that to make a one element tuple you need to add a comma after the one value! (1) would not work!

t = tuple([1, 2, 3])

t = (1, 2, 3)

t = (1,)

CSE 160: Tuples & Mutability

6 of 18

Lecture Review: Mutability

CSE 160: Tuples & Mutability

7 of 18

What is Mutable?

Mutable = Can Change

A mutable object can be changed after it is created.

Example:

my_list = [1, 2, 3]

my_list.append(4)

print(my_list)

Output:

[1, 2, 3, 4]

The list changed directly.

Common Mutable Types:

  • list�
  • dictionary (dict)�

CSE 160: Tuples & Mutability

8 of 18

What is Immutable?

Immutable = Cannot Change

An immutable object cannot be changed after it is created.

Example:

x = 5

x = x + 1

print(x)

Output:

6

The original 5 was not changed.� Python created a new object 6.

Common Immutable Types:

  • int�
  • float�
  • string (str)�
  • tuple�
  • bool

CSE 160: Tuples & Mutability

9 of 18

A tuple is an immutable sequence

  • Lists, strings, and tuples are all ordered sequences
  • Strings and tuples are immutable
  • The elements of a tuple can be anything
    • (including mutable types)

9

# Creating tuples

w = (4, 7, 9)

x = ("once", "upon", "a", "time")

y = () # An empty tuple

z = "rea2000", "blue" # parenthesis can make things � # clearer but are often not needed

CSE 160: Tuples & Mutability

10 of 18

Section Handout Problems

CSE 160: Tuples & Mutability

11 of 18

Problem 1

A marathon race has recorded the finishing results of several runners.

Each runner's information is stored in a tuple in the format:

(runner_name, country, finish_time_minutes)

Your goal is to:

Part A:

1. Print each runner’s name and finish time.

Part B:

2. Determine the fastest runner and their time.

You must use indexing

CSE 160: Tuples & Mutability

12 of 18

Problem 2

Write code that does the following, given study_hours which is a dictionary where the key is the subject and the value is the number of hours studied.:

  1. Prints each subject and the number of hours studied
  2. Labels the subject "Good" if the hours are 4 or more, otherwise "Needs more practice"
  3. Calculates the total number of hours studied
  4. Counts how many subjects are labeled "Good"

study_hours = {

"Math": 4,

"English": 3,

"Science": 6,

"History": 2,

"Computer Science": 5

}

Expected Output:

Math: 4 hours (Good)

English: 3 hours (Needs more practice)

Science: 6 hours (Good)

History: 2 hours (Needs more practice)

Computer Science: 5 hours (Good)

CSE 160: Tuples & Mutability

13 of 18

Problem 3

Write a function called analyze_scores(score_list) that takes in one parameter score_list, which is a list of student scores. The function should do the following:

  • Find the highest score (highest_score)
  • Find the lowest score (lowest_score)
  • Count how many students scored 80 or higher (count_80_or_more)

The function must return all three values as a tuple in this order:

(highest_score, lowest_score, count_80_or_more)

Example call to function:

scores = [88, 72, 90, 65, 78, 94, 55, 83, 100, 67, 81]

highest_score, lowest_score, count_80_or_more = analyze_scores(scores)

Expected output:

(100, 55, 6)

CSE 160: Tuples & Mutability

14 of 18

Problem 4A

Problem 4. Part A and Part B refer to the code below.

list_1 = [0, 5, 10]

list_2 = ['a', 'z']

list_3 = [100]

print("Before, list_1:", list_1)

print("Before, list_2:", list_2)

print("Before, list_3:", list_3)

def test_mutability(a, b, c):

a[0] = [3]

b = ['b']

c = [c[0] + 1]

test_mutability(list_1, list_2, list_3)

print("After, list_1:", list_1)

print("After, list_2:", list_2)

print("After, list_3:", list_3)

Part A. What does the following code output?

CSE 160: Tuples & Mutability

15 of 18

Problem 4A

Problem 4. Part A and Part B refer to the code below.

list_1 = [0, 5, 10]

list_2 = ['a', 'z']

list_3 = [100]

print("Before, list_1:", list_1)

print("Before, list_2:", list_2)

print("Before, list_3:", list_3)

def test_mutability(a, b, c):

a[0] = [3]

b = ['b']

c = [c[0] + 1]

test_mutability(list_1, list_2, list_3)

print("After, list_1:", list_1)

print("After, list_2:", list_2)

print("After, list_3:", list_3)

Part A. What does the following code output?

CSE 160: Tuples & Mutability

16 of 18

Problem 4B

Problem 4. Part A and Part B refer to the code below.

list_1 = [0, 5, 10]

list_2 = ['a', 'z']

list_3 = [100]

print("Before, list_1:", list_1)

print("Before, list_2:", list_2)

print("Before, list_3:", list_3)

def test_mutability(a, b, c):

a[0] = [3]

b = ['b']

c = [c[0] + 1]

test_mutability(list_1, list_2, list_3)

print("After, list_1:", list_1)

print("After, list_2:", list_2)

print("After, list_3:", list_3)

Part B. What happened? Why is this the output once the test_mutability is called?

CSE 160: Tuples & Mutability

17 of 18

Problem 4B

Problem 4. Part A and Part B refer to the code below.

list_1 = [0, 5, 10]

list_2 = ['a', 'z']

list_3 = [100]

print("Before, list_1:", list_1)

print("Before, list_2:", list_2)

print("Before, list_3:", list_3)

def test_mutability(a, b, c):

a[0] = [3]

b = ['b']

c = [c[0] + 1]

test_mutability(list_1, list_2, list_3)

print("After, list_1:", list_1)

print("After, list_2:", list_2)

print("After, list_3:", list_3)

Part B. What happened? Why is this the output once the test_mutability is called?

CSE 160: Tuples & Mutability

18 of 18

Written Check-In

CSE 160: Tuples & Mutability