Python 3.10 Lists

The notebook includes Popcorn Hacks and Homework for list indexing, accumulators, counts counting, and filtering.

Popcorn Hack 1: Predict, Run, and Fix the Crash

Code Runner Challenge

Lists Popcorn 1 - predict, run, fix the crash

View IPYNB Source
# CODE_RUNNER: Lists Popcorn 1 - predict, run, fix the crash
# Using valid indices avoids the out-of-range crash while still demonstrating first and last access.
# Practice first, last, and length-based access on the favorite_foods list.

favorite_foods = ["pizza", "sushi", "tacos", "ramen"]

print(favorite_foods[0])      # Prediction: pizza
print(favorite_foods[-1])     # Prediction: ramen
print(len(favorite_foods))    # Prediction: 4
print(favorite_foods[3])      # Fixed from an out-of-range index; prediction: ramen
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Sum and Average

Code Runner Challenge

Lists Popcorn 2

View IPYNB Source
# CODE_RUNNER: Lists Popcorn 2
# Add each number with an accumulator instead of using sum().
# Check for an empty list before dividing so the average calculation is safe.

ratings = [7, 6, 8, 5, 7, 9]

total = 0
for number in ratings:
    total += number

average = total / len(ratings)

print("Total:", total)
print("Average:", average)

sleep_hours = []
if len(sleep_hours) == 0:
    print("No data yet")
else:
    sleep_hours_total = 0
    for hours in sleep_hours:
        sleep_hours_total += hours
    print("Sleep average:", sleep_hours_total / len(sleep_hours))
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3: Count Then Filter

Code Runner Challenge

Lists Popcorn 3 - count then filter

View IPYNB Source
# CODE_RUNNER: Lists Popcorn 3 - count then filter
# Count each season in a dictionary as it appears in the list.
# Build a second list containing only seasons that appear at least twice.

seasons = ["winter", "summer", "fall", "summer", "spring", "summer", "fall"]

counts = {}
for season in seasons:
    if season in counts:
        counts[season] += 1
    else:
        counts[season] = 1

frequent_choices = []
for season in counts:
    if counts[season] >= 2:
        frequent_choices.append(season)

print(counts)
print(frequent_choices)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack: Indexing, Accumulator, Count, and Filter

Code Runner Challenge

Lists Homework - indexing, accumulator, count and filter on one dataset

View IPYNB Source
# CODE_RUNNER: Lists Homework - indexing, accumulator, count and filter on one dataset
# snack_votes stores snack choices and snack_prices stores the related numeric data.
# The sections below demonstrate indexing, accumulation, counting, and filtering.

snack_votes = ["chips", "cookies", "chips", "fruit", "cookies", "chips",
         "pretzels", "cookies", "chips", "fruit", "chips", "cookies"]

snack_prices = [2, 3, 2, 4, 3, 2, 1, 3, 2, 4, 2, 3]

# LESSON 1 - indexing
print(len(snack_votes))
print(snack_votes[-1])

# LESSON 2 - sum and average
total = 0
for price in snack_prices:
    total += price

average = total / len(snack_prices)
print("Total:", int(total))
print("Average:", int(average))

# LESSON 3 - count and filter
counts = {}
for vote in snack_votes:
    if vote in counts:
        counts[vote] += 1
    else:
        counts[vote] = 1

print(counts)

frequent_choices = []
for snack in counts:
    if counts[snack] >= 3:
        frequent_choices.append(snack)

print(frequent_choices)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...