Lab

6.4.2. Lab#

  • Use this lab to practice the chapter’s coding skills in runnable Python code.

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

  • Launch Live Code (the rocket) to write and run code 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.

Hide code cell source

### Solution
numbers = [3, 8, 15, 22, 4, 19, 11]

squares = [n ** 2 for n in numbers]
evens = [n for n in numbers if n % 2 == 0]

print(f"Squares: {squares}")
print(f"Evens: {evens}")
Squares: [9, 64, 225, 484, 16, 361, 121]
Evens: [8, 22, 4]
### 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.

Hide code cell source

### Solution
leaderboard = (100, 95, 88, 76, 65, 50)

top, *middle, bottom = leaderboard
print(f"Top: {top}")
print(f"Middle: {middle}")
print(f"Bottom: {bottom}")

medals = [("gold", "Alice"), ("silver", "Bob"), ("bronze", "Cara")]
for place, name in medals:
    print(f"{place}: {name}")
Top: 100
Middle: [95, 88, 76, 65]
Bottom: 50
gold: Alice
silver: Bob
bronze: Cara
### 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.

Hide code cell source

### Solution
readings = [6500, 9100, -1, 7200, 8800]

readings.remove(-1)
readings.append(8200)
top_three = sorted(readings)[::-1][:3]

print(f"Readings: {readings}")
print(f"Top three: {top_three}")
print(f"Count: {len(readings)}")
Readings: [6500, 9100, 7200, 8800, 8200]
Top three: [9100, 8800, 8200]
Count: 5
### 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.

Hide code cell source

### Solution
original = [10, 20, 30]
alias = original
copy = original[:]

alias.append(40)
copy.append(99)

print(f"Original: {original}")
print(f"Alias: {alias}")
print(f"Copy: {copy}")
Original: [10, 20, 30, 40]
Alias: [10, 20, 30, 40]
Copy: [10, 20, 30, 99]
### 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.

Hide code cell source

### Solution
lines = ["North,Jan,112", "South,Jan,98", "West,Jan,144", "South,Feb,101"]
cutoff = 110

sales = []
for line in lines:
    region, month, amount = line.split(",")
    sales.append((region, month, int(amount)))

big_sales = [region for region, month, amount in sales if amount >= cutoff]

def amount_of(sale):
    return sale[2]

region, month, amount = max(sales, key=amount_of)

print(f"Big sales: {big_sales}")
print(f"Largest: {region} {month} {amount}")
Big sales: ['North', 'West']
Largest: West Jan 144