Popcorn Hack 1
movies = ['minecraft movie', 'star wars', 'the matrix', 'interstellar']
print(movies)
movies[1] = 'spiderman'
print(movies)
movies.append('endgame')
print(movies)
['minecraft movie', 'star wars', 'the matrix', 'interstellar']
['minecraft movie', 'spiderman', 'the matrix', 'interstellar']
['minecraft movie', 'spiderman', 'the matrix', 'interstellar', 'endgame']
Popcorn Hack 2
ages = [15, 20, 34, 16, 18, 21, 14, 19]
ages_voting = [num for num in ages if num >= 18]
print(ages_voting)
[20, 34, 18, 21, 19]
Homework Hack 1
Notes on - Python Lists || Python Tutorial || Learn Python Programming
- lists can be combined using the plus sign
- lists can have duplicate values (unlike sets)
- lists can have different data types like
- you can access lists with indexes (starting at 0)
- slicing retrieves a range of values
- append, slice, and combine all can alter lists
- lists are created with brackets
- lists can have other lists inside of them
Notes on - Python Tutorial: Comprehensions - How they work and why you should be using them
- comprehensions make creating lists easy
- allows you to create new lists by apply an expression to each item
- you can use conditional statements to modify or filter lists
- dictionary comprehensions used for creating dictionaries
- set comprehensions are use to create sets
- nested comprehensions used to handle complex data structures (list of lists)
- comprehensions provide more efficient code by reducing need for loops
Homework Hack 2
original_list = list(range(1, 31))
filtered_list = [num for num in original_list if num % 3 == 0 and num % 5 != 0]
print("Original List:")
print(original_list)
print("\nFiltered List (Divisible by 3 but not by 5):")
print(filtered_list)
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 (Divisible by 3 but not by 5):
[3, 6, 9, 12, 18, 21, 24, 27]
Homework Hack 3
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]
data_sorted = filtered_data.sort_values(by="Total Streams (Millions)", ascending=False)
new_list = data_sorted["Total Streams (Millions)"].tolist()
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]
Review Questions
- Q: Explain what lists are in Python, including how to modify and manipulate them.
- A: Lists in Python are ordered collections of items that can store different types of data, such as numbers, strings, or even other lists. They are mutable, meaning you can change their contents. You can add items using append() or insert(), remove them with remove() or pop(), and access or modify items by their index (e.g., my_list[0] = “new value”). Lists can also be sliced, sorted, or filtered using loops or list comprehensions.
- Q: Provide a real-world scenario where a filtering algorithm might be applied.
- A: A music streaming service like Spotify might use a filtering algorithm to show users songs with more than 10 million streams, or to recommend songs in a specific genre with high user ratings.
- Q: Discuss why analyzing the efficiency of filtering algorithms is important for software development.
- A: Efficiency matters because filtering large datasets can be time consuming and resource intensive. Analyzing an algorithm’s efficiency helps developers choose or design algorithms that minimize processing time and memory usage, leading to faster applications and better user experiences. Especially when working with real time systems or big data.