Skip to the content.

manas

team teaach

# Popcorn Hack #1

# Create a list of four favorite movies
movies = ["The Dark Knight", "Inception", "Interstellar", "The Matrix"]
print("Original list:", movies)

# Replace the second movie (index 1) with a new movie
movies[1] = "Avengers: Endgame"
print("After replacing second movie:", movies)

# Add another movie to the list
movies.append("Spider-Man: Into the Spider-Verse")
print("After adding another movie:", movies)

# Display the final updated list
print("\nFinal movie list:")
for i, movie in enumerate(movies, 1):
    print(f"{i}. {movie}")
Original list: ['The Dark Knight', 'Inception', 'Interstellar', 'The Matrix']
After replacing second movie: ['The Dark Knight', 'Avengers: Endgame', 'Interstellar', 'The Matrix']
After adding another movie: ['The Dark Knight', 'Avengers: Endgame', 'Interstellar', 'The Matrix', 'Spider-Man: Into the Spider-Verse']

Final movie list:
1. The Dark Knight
2. Avengers: Endgame
3. Interstellar
4. The Matrix
5. Spider-Man: Into the Spider-Verse
# Popcorn Hack #2

# Given list of ages
ages = [15, 20, 34, 16, 18, 21, 14, 19]
print("Original list of ages:", ages)

# Method 1: Using a for loop to create a new list with voting-eligible ages
voting_ages = []
for age in ages:
    if age >= 18:
        voting_ages.append(age)
        
print("Voting eligible ages (using for loop):", voting_ages)

# Method 2: Using list comprehension (more Pythonic)
voting_ages_comprehension = [age for age in ages if age >= 18]
print("Voting eligible ages (using list comprehension):", voting_ages_comprehension)

# Count how many people are eligible to vote
print(f"\nNumber of people eligible to vote: {len(voting_ages)}")
print(f"Percentage eligible to vote: {len(voting_ages)/len(ages)*100:.1f}%")
Original list of ages: [15, 20, 34, 16, 18, 21, 14, 19]
Voting eligible ages (using for loop): [20, 34, 18, 21, 19]
Voting eligible ages (using list comprehension): [20, 34, 18, 21, 19]

Number of people eligible to vote: 5
Percentage eligible to vote: 62.5%

📚 Homework Hacks

✅ Homework Hack #1: Watch both videos and write notes

Instructions:

  • Write 3 bullet points per video
  • Bonus if you write more than 6 bullet points per video

🎥 Video 1: Python Lists

  • 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: Python Dictionaries

  • 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: Numbers divisible by 3 but not by 5

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

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

# Step 3: Print both the original and filtered lists
print("Original list of numbers from 1 to 30:")
print(original_numbers)

print("\nNumbers divisible by 3 but not by 5:")
print(filtered_numbers)

# Alternative approach using a for loop instead of list comprehension
filtered_numbers_loop = []
for num in original_numbers:
    if num % 3 == 0 and num % 5 != 0:
        filtered_numbers_loop.append(num)

print("\nSame result using a for loop:")
print(filtered_numbers_loop)

# Print each number with its divisibility explanation
print("\nExplanation for filtered numbers:")
for num in filtered_numbers:
    print(f"{num} is divisible by 3 but not by 5")
Original list of numbers from 1 to 30:
[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]

Numbers divisible by 3 but not by 5:
[3, 6, 9, 12, 18, 21, 24, 27]

Same result using a for loop:
[3, 6, 9, 12, 18, 21, 24, 27]

Explanation for filtered numbers:
3 is divisible by 3 but not by 5
6 is divisible by 3 but not by 5
9 is divisible by 3 but not by 5
12 is divisible by 3 but not by 5
18 is divisible by 3 but not by 5
21 is divisible by 3 but not by 5
24 is divisible by 3 but not by 5
27 is divisible by 3 but not by 5
# Homework Hack #3: Filter Spotify Data

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.