Homework

6.4.3. Homework#

  • Use these questions after class to check your understanding of the chapter concepts and coding skills.

  • Sign in from the account menu before you start working on the homework.

  • Complete all true/false and coding questions before you submit the homework.

  • Launch Live Code (the rocket) to write and run code in the coding cells.

  • Multiple submissions are allowed. The highest score is kept.

1. Lists

A project tracker that needs to preserve task order and allow updates is a good fit for a list.

Hide code cell source

### Solution
# True
# A list keeps its items in insertion order and is mutable, so you can append new tasks,
# reorder them, or update an item by index.

2. Tuples

A function can return a tuple when it needs to return two related values such as a minimum and a maximum.

Hide code cell source

### Solution
# True
# Writing return low, high packs the two values into one tuple. The caller can then unpack
# them, as in low, high = min_max(values).

3. Tuples

You can change the second item of a tuple with t[1] = 5 because tuples are ordered.

Hide code cell source

### Solution
# False
# Tuples are ordered, so you can read t[1], but they are immutable. Assigning t[1] = 5
# raises a TypeError; to change a value you build a new tuple or use a list.

4. Aliasing

If two variables refer to the same mutable list, changing the list through one variable can be visible through the other.

Hide code cell source

### Solution
# True
# Assignment such as b = a copies the reference, not the list, so both names are aliases
# for the same object. A change made through one name, such as b.append(4), shows up
# through the other.

5. Shallow Copies

After b = a.copy(), appending to a nested list inside b never changes a.

Hide code cell source

### Solution
# False
# a.copy() makes a shallow copy: the outer list is new, but nested lists are still shared
# between a and b. Appending to a nested list through b therefore changes a as well;
# copy.deepcopy() would copy the nested lists too.
### 6. Update an ordered list
# Add `"review"` to the end of the tasks list and print exactly this line:
# # Tasks: ['collect', 'clean', 'review']
tasks = ["collect", "clean"]

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
tasks = ["collect", "clean"]
tasks.append("review")
print("Tasks:", tasks)
Tasks: ['collect', 'clean', 'review']
### 7. Unpack tuple records
# Unpack the record and print exactly these three lines:
# # Name: Ana
# # Score: 91
# # Passed: True
record = ("Ana", 91, True)

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
record = ("Ana", 91, True)
name, score, passed = record
print("Name:", name)
print("Score:", score)
print("Passed:", passed)
Name: Ana
Score: 91
Passed: True
### 8. Slice a list
# Use slicing to print the first three and last two numbers. Print exactly these two lines:
# # First three: [3, 8, 1]
# # Last two: [7, 4]
nums = [3, 8, 1, 9, 7, 4]

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
nums = [3, 8, 1, 9, 7, 4]
print("First three:", nums[:3])
print("Last two:", nums[-2:])
First three: [3, 8, 1]
Last two: [7, 4]
### 9. Compare copied lists
# Show aliasing versus copying with these steps:
# 1. Make an alias with alias = original.
# 2. Make a copy with backup = original.copy().
# 3. Append 30 through the alias with alias.append(30).
# 4. Print the alias, then the copy.
# Print exactly these two lines:
# # Alias: [10, 20, 30]
# # Copy: [10, 20]
original = [10, 20]

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
original = [10, 20]
alias = original
backup = original.copy()
alias.append(30)
print("Alias:", alias)
print("Copy:", backup)
Alias: [10, 20, 30]
Copy: [10, 20]
### 10. Count with a comprehension
# Use sum() with a list comprehension to count scores of 60 or above. Print exactly this line:
# # Passing: 4 of 5
scores = [72, 55, 88, 61, 90]

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
scores = [72, 55, 88, 61, 90]
passing = sum([score >= 60 for score in scores])
print(f"Passing: {passing} of {len(scores)}")
Passing: 4 of 5