Skip to the content.

Group 8 Homework

Big Idea 3

Popcorn Hack #1

# Create a list of four famous movies
movies = ["The Godfather", "Titanic", "Inception", "The Dark Knight"]

# Replace the second movie (index 1) with a new movie
movies[1] = "Forrest Gump"

# Add another movie to the end of the list
movies.append("Pulp Fiction")

# Display the updated list
print(movies)

Popcorn Hack 2

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

# Create a new list with ages 18 or older
eligible_voters = [age for age in ages if age >= 18]

print(eligible_voters)

Homework Hack #1

Video Notes: Python Lists Python Tutorial Learn Python Programming

  • Python lists store ordered data, and order is important (unlike sets).

  • Lists can be created using the list constructor or by using brackets.

  • Values can be added to a list using the “append” method.

  • List elements are indexed starting from 0, and negative indices wrap around from the end.

  • You can access a range of values in a list using slicing (e.g., list[start:stop]).

  • Lists can contain different data types (integers, strings, booleans, etc.) and even other lists.

  • You can combine lists using concatenation (+), but order matters when combining them.

Video Notes: Python Tutorial: Comprehensions - How they work and why you should be using them

  • List Comprehensions simplify code by allowing you to create lists in a single line, making it more readable and concise compared to traditional for loops.

  • Square of Numbers: A simple list comprehension can easily calculate the square of numbers in a list (n * n for n in nums).

  • Maps and Lambdas: While map() and lambda functions can achieve similar results, list comprehensions are more readable for beginners and often preferred over these.

  • Filtering Even Numbers: List comprehensions can include conditions, such as filtering even numbers (if n % 2 == 0), making them versatile for transforming lists based on conditions.

  • Nested Loops: List comprehensions support nested loops, which allows you to generate complex combinations like letter-number pairs from two lists (e.g., A0, A1, A2).

  • Dictionary Comprehension: You can use comprehensions for dictionaries too, like creating a dictionary from two lists (e.g., names and superhero names) with name: hero.

  • Set Comprehensions: Just like list comprehensions, set comprehensions can be used to generate sets with unique values by eliminating duplicates automatically.

Homework Hack #2

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

# Filter numbers divisible by 3 but not by 5
filtered_nums = [n for n in nums if n % 3 == 0 and n % 5 != 0]

# Print the original and filtered lists clearly
print("Original List:", nums)
print("Filtered List (divisible by 3 but not 5):", filtered_nums)

Homework Hack #3

def filter_long_names(names, min_length):
    # Filter and return names that meet or exceed the minimum length
    return [name for name in names if len(name) >= min_length]

# Test the function with a list of names
names_list = ["John", "Alexander", "Amy", "Elizabeth", "Kate"]
min_length = 5

# Call the function and print the result
filtered_names = filter_long_names(names_list, min_length)
print("Filtered Names:", filtered_names)

Review Questions

  1. What are lists in Python, and how do you modify/manipulate them? Lists in Python are ordered, changeable collections used to store multiple items in one variable. You can modify them by changing elements directly using indexing, adding items with append() or insert(), removing items with remove() or pop(), and filtering or transforming them with list comprehensions.

  2. Real-world scenario for filtering algorithms: A streaming service may use a filtering algorithm to recommend movies only to users aged 18 or older. By filtering user profiles based on age, the platform ensures age-appropriate content is shown.

  3. Why analyzing filtering algorithm efficiency matters in software development: Efficient filtering algorithms ensure fast processing, especially with large data sets. This improves performance, scalability, and user experience. Analyzing efficiency helps developers choose solutions that can handle increasing data without slowing down.