Popcorn Hack 1
# Create and modify a movie list
movies = ["Inception", "Interstellar", "Shrek", "The Dark Knight"]
movies[1] = "Spider-Man: Across the Spider-Verse" # Replace second movie
movies.append("Everything Everywhere All at Once") # Add another movie
print("Updated Movie List:", movies)
Updated Movie List: ['Inception', 'Spider-Man: Across the Spider-Verse', 'Shrek', 'The Dark Knight', 'Everything Everywhere All at Once']
Popcorn Hack #2
# Filter for ages 18 or older
ages = [15, 20, 34, 16, 18, 21, 14, 19]
voting_ages = [age for age in ages if age >= 18]
print("Eligible to Vote:", voting_ages)
Eligible to Vote: [20, 34, 18, 21, 19]
Homework hacks Video 1: Complete Python List Tutorial Lists are ordered, mutable sequences in Python.
You can use indexing and slicing to access elements.
append(), insert(), remove(), and pop() are key list methods.
Lists can hold multiple data types at once.
Negative indexing accesses elements from the end.
List comprehensions offer a fast way to filter or transform lists.
Nested lists allow 2D-like structures in Python.
Video 2: How to Filter Lists in Python Filtering is done with list comprehensions using conditional logic.
if clauses inside list comprehensions make filtering efficient.
Filtering is commonly used in data science and real-world analytics.
Python’s filter() function can also be used, paired with lambda functions.
Filtering has a linear time complexity O(n).
Works well on any iterable like lists, tuples, or sets.
Filtering is cleaner and faster with list comprehension compared to loops.
Homework hack 2
# Create and filter numbers from 1 to 30
numbers = list(range(1, 31))
filtered = [num for num in numbers if num % 3 == 0 and num % 5 != 0]
print("Original List:", numbers)
print("Filtered List (Div by 3, Not by 5):", filtered)
Original List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
Filtered List (Div by 3, Not by 5): [3, 6, 9, 12, 18, 21, 24, 27]
Homework Hack 3
import pandas as pd
df = pd.read_csv("spotify_data.csv")
print(df.columns)
Index(['Country', 'Artist', 'Album', 'Genre', 'Release Year',
'Monthly Listeners (Millions)', 'Total Streams (Millions)',
'Total Hours Streamed (Millions)', 'Avg Stream Duration (Min)',
'Platform Type', 'Streams Last 30 Days (Millions)', 'Skip Rate (%)'],
dtype='object')
import pandas as pd
def filter_spotify_data():
df = pd.read_csv("spotify_data.csv")
# Filter songs with over 10 million total streams
filtered_songs = df[df["Total Streams (Millions)"] > 10]
print("Songs with over 10M streams:")
print(filtered_songs[["Artist", "Album", "Total Streams (Millions)"]]) # Just key info
filter_spotify_data()
Songs with over 10M streams:
Artist Album Total Streams (Millions)
0 Taylor Swift 1989 (Taylor's Version) 3695.53
1 The Weeknd After Hours 2828.16
2 Post Malone Austin 1425.46
3 Ed Sheeran Autumn Variations 2704.33
4 Ed Sheeran Autumn Variations 3323.25
.. ... ... ...
495 Karol G MAÑANA SERÁ BONITO 2947.97
496 Dua Lipa Future Nostalgia 4418.61
497 Karol G MAÑANA SERÁ BONITO 2642.90
498 SZA SOS 4320.23
499 BTS Proof 4804.15
[500 rows x 3 columns]