CSE 160 Section 7
Tuples & Mutability!
CSE 160: Tuples & Mutability
Logistics
CSE 160: Tuples & Mutability
Lecture Review: Tuples
CSE 160: Tuples & Mutability
Tuples
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
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
Lecture Review: Mutability
CSE 160: Tuples & Mutability
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:
CSE 160: Tuples & Mutability
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:
CSE 160: Tuples & Mutability
A tuple is an immutable sequence
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
Section Handout Problems
CSE 160: Tuples & Mutability
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
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.:
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
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:
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
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
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
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
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
Written Check-In
CSE 160: Tuples & Mutability