6.4.2. Lab#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
### Question 1: List comprehensions
# Using the list numbers, build two new lists with list comprehensions:
# squares: each number raised to the second power.
# evens: only the numbers that are divisible by 2.
# Print exactly these two lines:
# Squares: [9, 64, 225, 484, 16, 361, 121]
# Evens: [8, 22, 4]
numbers = [3, 8, 15, 22, 4, 19, 11]
### Your code starts here.
### Your code ends here.
### Question 2: Tuple unpacking
# 1. Unpack leaderboard into top (first score), middle (a list of the
# scores in between), and bottom (last score) using starred unpacking.
# Print top, middle, and bottom.
# 2. Loop over medals, a list of (place, name) tuples, unpacking each pair
# directly in the for statement, printing "place: name" for each.
# Print exactly these six lines:
# Top: 100
# Middle: [95, 88, 76, 65]
# Bottom: 50
# gold: Alice
# silver: Bob
# bronze: Cara
leaderboard = (100, 95, 88, 76, 65, 50)
medals = [("gold", "Alice"), ("silver", "Bob"), ("bronze", "Cara")]
### Your code starts here.
### Your code ends here.
### Question 3: Clean up a list of readings
# readings holds daily step counts, including an invalid reading of -1.
# Remove the -1 with .remove(), then append a new reading of 8200.
# Build top_three by sorting readings with sorted(), reversing the
# result with slicing, and keeping the first three values.
# Print exactly these three lines:
# Readings: [6500, 9100, 7200, 8800, 8200]
# Top three: [9100, 8800, 8200]
# Count: 5
readings = [6500, 9100, -1, 7200, 8800]
### Your code starts here.
### Your code ends here.
### Question 4: Aliasing versus copying
# original is a list. alias points to the SAME list; copy is an
# independent copy made with slicing (original[:]).
# Append 40 to alias, then append 99 to copy.
# Print original, alias, and copy to see which lists changed together.
# Print exactly these three lines:
# Original: [10, 20, 30, 40]
# Alias: [10, 20, 30, 40]
# Copy: [10, 20, 30, 99]
original = [10, 20, 30]
### Your code starts here.
### Your code ends here.
### Question 5: Sales records as tuples
# Each string in lines describes one sale: region, month, amount.
# Build sales, a list of (region, month, amount) tuples, converting
# amount to an int.
# Build big_sales: a list (comprehension) of the regions whose amount
# is at least cutoff.
# Find the largest sale with max() and a key function that returns
# the amount, then unpack it into region, month, and amount.
# Print exactly these two lines:
# Big sales: ['North', 'West']
# Largest: West Jan 144
lines = ["North,Jan,112", "South,Jan,98", "West,Jan,144", "South,Feb,101"]
cutoff = 110
### Your code starts here.
### Your code ends here.
Question 1
Question 2
Question 3
Question 4
Question 5