Homework Hack #1
My notes from both videos
- Python lists store data in an ordered sequence, allowing indexing, slicing, and preservation of duplicate values.
- You can create lists using square brackets or the list() constructor
- Lists can be modified by using methods like append(), extend(), and + for concatenation.
- Access list elements using zero-based indexing or negative indices
- Use slicing to grab sublists without altering the original.
- Lists can hold integers, strings, booleans, even other lists
- List comprehensions provide readable one-line alternatives to for loops for making lists
- comprehensions are easier to read and better for beginners and collaboration
- you can use if statements to filter through list and dictionaries
- you can have nested loops with lists
Homework Hack #2
Homework Hack #2: Create Python code that:
Initializes a list containing numbers from 1 to 30. Filters out and displays numbers divisible by 3 but not by 5. Prints both the original and filtered lists clearly.
numbers = [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]
new_numbers = [num for num in numbers if num % 3 == 0 and num % 5 != 0]
print(new_numbers)
[3, 6, 9, 12, 18, 21, 24, 27]
Homework Hack #3
Homework Hack #3 Develop a function named filter_spotify_data that:
Reads data from the Spotify Global Streaming Data 2024 CSV. Filters and returns songs with over 10 million streams. Clearly display the filtered data in your program.
import pandas as pd
data = pd.read_csv("Spotify_2024_Global_Streaming_Data.csv")
data_cleaned = data.dropna()
filtered_data = data_cleaned[data_cleaned["Total Streams (Millions)"] > 10]
# Sort the filtered data by total streams
data_sorted = filtered_data.sort_values(by="Total Streams (Millions)", ascending=False)
# Convert to list
new_list = data_sorted["Total Streams (Millions)"].tolist()
# Display
print("First few rows of sorted data:")
print(data_sorted)
First few rows of sorted data:
Country Artist Album Genre \
473 Spain Billie Eilish Happier Than Ever Classical
294 France BTS Proof Jazz
156 Germany BTS Proof K-pop
22 Sweden BTS Proof EDM
251 Sweden BTS Proof Indie
.. ... ... ... ...
175 Russia Karol G MAÑANA SERÁ BONITO Classical
216 India Ed Sheeran Autumn Variations Classical
16 United States Ed Sheeran Autumn Variations Jazz
308 Turkey Ariana Grande Eternal Sunshine Rock
454 South Korea Doja Cat Scarlet K-pop
Release Year Monthly Listeners (Millions) Total Streams (Millions) \
473 2019 56.56 4985.54
294 2023 6.53 4982.14
156 2023 45.18 4982.01
22 2023 95.77 4977.34
251 2018 44.47 4970.09
.. ... ... ...
175 2020 48.01 132.35
216 2021 66.27 127.87
16 2019 59.26 85.59
308 2019 6.40 68.89
454 2022 37.34 53.56
Total Hours Streamed (Millions) Avg Stream Duration (Min) Platform Type \
473 17691.17 2.98 Premium
294 14641.63 4.19 Premium
156 15300.52 4.33 Premium
22 17975.85 3.94 Free
251 20135.14 2.97 Premium
.. ... ... ...
175 418.55 2.72 Premium
216 455.78 3.32 Free
16 335.14 2.62 Free
308 184.30 4.15 Premium
454 202.25 3.26 Free
Streams Last 30 Days (Millions) Skip Rate (%)
473 155.58 13.61
294 71.61 33.73
156 40.84 6.29
22 38.79 9.49
251 172.35 24.80
.. ... ...
175 117.16 32.07
216 106.35 11.71
16 181.18 34.53
308 40.74 2.00
454 176.70 25.17
[500 rows x 12 columns]