Python 3.10 Lists

Completed the Popcorn Hacks and Homework for list indexing, accumulators, frequency 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 only valid indices avoids the out-of-range crash.

games = ["Zelda", "Mario", "Tetris", "Minecraft"]

print(games[0])      # Prediction: Zelda
print(games[-1])     # Prediction: Minecraft
print(len(games))    # Prediction: 4
print(games[3])      # Fixed: games[4] would crash because the last index is 3
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().
# An empty list is checked first so we never divide by zero.

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

total = 0
for hours in practice_hours:
    total += hours

print("Total:", total)
print("Average:", total / len(practice_hours))

new_player = []
if len(new_player) == 0:
    print("No data yet")
else:
    print("Average:", sum(new_player) / len(new_player))
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 how many times each genre appears, then keep genres that appear at least twice.

genres = ["rpg", "puzzle", "racing", "puzzle", "rpg", "puzzle", "shooter"]

counts = {}
for genre in genres:
    if genre in counts:
        counts[genre] += 1
    else:
        counts[genre] = 1

popular = []
for genre in counts:
    if counts[genre] >= 2:
        popular.append(genre)

print(counts)
print(popular)
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
# pets holds each student's favorite pet and ages holds the related numbers.

pets = ["dog", "cat", "dog", "fish", "cat", "dog",
        "bird", "cat", "dog", "fish", "dog", "cat"]
ages = [8, 12, 9, 10, 11, 8, 13, 12, 9, 10, 8, 11]

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

# LESSON 2 - sum and average
total = 0
for age in ages:
    total += age
print("Total:", total)
print("Average:", total / len(ages))

# LESSON 3 - count and filter
counts = {}
for pet in pets:
    if pet in counts:
        counts[pet] += 1
    else:
        counts[pet] = 1
print(counts)

favorites = []
for pet in counts:
    if counts[pet] >= 3:
        favorites.append(pet)
print(favorites)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...