popcorn hacks:

Popcorn 1:

# Create a list of favorite movies movies = ["Inception", "The Matrix", "Interstellar", "The Dark Knight"]

# Replace the second movie with a new one movies[1] = "Spider-Man: Into the Spider-Verse"

# Add another movie to the list movies.append("Everything Everywhere All At Once")

# Display the updated list print(movies)

Popcorn 2:

ages = [15, 20, 34, 16, 18, 21, 14, 19]

# Create a new list with only voting-eligible ages voting_ages = [age for age in ages if age >= 18]

# Display the new list print(voting_ages)

Homework Hack 1 NOTES:

Video 1:

  • Lists are ordered collections; order matters and is preserved.

  • Created using square brackets ([]) or the list() constructor.

  • Values can be added with .append(), which adds to the end.

  • Access elements using indices starting from 0; negative indices count from the end (-1 is the last item).

  • Lists support slicing: [start:stop] returns a sublist (includes start, excludes stop).

  • Lists can store different data types: integers, strings, even other lists.

Video 2:

  • Dictionaries store data in key-value pairs.

  • Created using curly braces ({}) or the dict() constructor.

  • Keys must be unique and immutable (e.g., strings or numbers).

  • Values are accessed using the key: my_dict[“key”]

  • Add or update values with assignment: my_dict[“new_key”] = value

  • Use .get(“key”) to safely access a key without errors if it doesn’t exist

Homework Hack 2:

# Initialize a list containing numbers from 1 to 30
numbers = list(range(1, 31))

# Filter numbers divisible by 3 but not by 5
filtered_numbers = [num for num in numbers if num % 3 == 0 and num % 5 != 0]

# Print the original list
print("Original list:")
print(numbers)

# Print the filtered list
print("\nFiltered list (divisible by 3 but not by 5):")
print(filtered_numbers)
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
import matplotlib.pyplot as plt

def filter_spotify_data(file_path, min_streams=10):
    """
    Filter Spotify streaming data based on minimum stream count
    
    Args:
        file_path: Path to the Spotify data CSV file
        min_streams: Minimum number of streams required (in millions)
        
    Returns:
        DataFrame containing filtered data
    """
    # Read the CSV file
    try:
        df = pd.read_csv(file_path)
        
        # Filter data for songs with streams above the threshold
        filtered_df = df[df['Total Streams (Millions)'] > min_streams]
        
        # Sort by total streams in descending order
        filtered_df = filtered_df.sort_values(by='Total Streams (Millions)', ascending=False)
        
        return filtered_df
    except Exception as e:
        print(f"Error processing file: {e}")
        return pd.DataFrame()  # Return empty DataFrame if there's an error

# Path to the dataset
file_path = "Spotify_2024_Global_Streaming_Data.csv"

# Filter data for songs with more than 10 million streams
popular_songs = filter_spotify_data(file_path)

# Display information about the filtered data
print(f"Total number of songs in dataset: {len(pd.read_csv(file_path))}")
print(f"Number of songs with streams over 10 million: {len(popular_songs)}")

if len(popular_songs) > 0:
    print("\nTop Most Streamed Songs:")
    print(popular_songs[['Artist', 'Album', 'Total Streams (Millions)', 'Country']].head())

    # Only create visualizations if we have data
    if len(popular_songs) >= 2:  # Need at least 2 rows for meaningful visualization
        # Create a simple bar chart of artists and their streams
        plt.figure(figsize=(10, 6))
        plt.bar(popular_songs['Artist'], popular_songs['Total Streams (Millions)'])
        plt.title('Artists by Total Streams')
        plt.xlabel('Artist')
        plt.ylabel('Total Streams (Millions)')
        plt.xticks(rotation=45, ha='right')
        plt.tight_layout()
        plt.show()
else:
    print("No songs found with streams over 10 million.")
Total number of songs in dataset: 500
Number of songs with streams over 10 million: 500

Top Most Streamed Songs:
            Artist              Album  Total Streams (Millions)  Country
473  Billie Eilish  Happier Than Ever                   4985.54    Spain
294            BTS              Proof                   4982.14   France
156            BTS              Proof                   4982.01  Germany
22             BTS              Proof                   4977.34   Sweden
251            BTS              Proof                   4970.09   Sweden

png

Review Questions:

  1. Explain what lists are in Python, including how to modify and manipulate them. Definition: Lists in Python are ordered, mutable collections of items that can hold elements of different data types (e.g., integers, strings, or even other lists). Creation: Lists are created using square brackets ([]) or the list() constructor. Modification: Add elements using .append() (to the end) or .insert() (at a specific index). Remove elements using .remove() (by value) or .pop() (by index). Replace elements by directly assigning a new value to a specific index (e.g., list[1] = “new_value”). Manipulation: Use slicing (list[start:stop]) to extract sublists. Use list comprehensions for concise filtering or transformations. Sort lists with .sort() (in-place) or sorted() (returns a new sorted list).

  2. Provide a real-world scenario where a filtering algorithm might be applied. Scenario: A filtering algorithm could be used in an e-commerce platform to display products based on user preferences. For example, filtering a list of products to show only those within a specific price range or with a minimum customer rating. Example: A user searches for laptops priced under $1000 with at least a 4-star rating. The platform applies a filtering algorithm to extract and display only the relevant products from the database.

  3. Discuss why analyzing the efficiency of filtering algorithms is important for software development. Performance: Efficient filtering algorithms ensure that applications can handle large datasets without significant delays, improving user experience. Scalability: As data grows, inefficient algorithms may lead to performance bottlenecks. Analyzing efficiency helps ensure the system can scale effectively. Resource Optimization: Efficient algorithms reduce computational resource usage (e.g., CPU and memory), which is critical for cost-effective and sustainable software. Real-Time Applications: In scenarios like financial trading or recommendation systems, filtering must be done in real-time. Analyzing efficiency ensures the system meets time constraints.