Study Plan: Python Lists and Filtering Algorithms
Overview
This study plan covers the concepts of Lists and Filtering Algorithms in Python. The goal is to understand how lists are used in Python, how to manipulate them, and how to apply filtering algorithms effectively.
Objectives
- Understand Python lists, their properties, and manipulation methods.
- Master list operations (access, modify, remove, append).
- Understand and apply filtering algorithms to extract data based on conditions.
- Evaluate the performance of filtering algorithms in Python.
Lesson Breakdown
Lesson 1: Introduction to Lists
-
Definition: A list is an ordered, mutable collection of elements in Python. It can store multiple data types.
- Creating a list:
example_list = [42, 3.1415, "Python", True] print(example_list) - Accessing List Elements:
- Positive indexing (starts from 0).
- Negative indexing (starts from -1 for the last element).
colors = ["red", "green", "blue", "yellow"] print(colors[2]) # Output: blue print(colors[-1]) # Output: yellow - Modifying Lists:
- Change an element:
colors[1] = "purple" - Add an element:
colors.append("orange") - Remove an element:
colors.remove("red")
print(colors) # Output: ['purple', 'blue', 'yellow', 'orange'] - Change an element:
- Creating a list:
Popcorn Hack #1 (5 minutes)
- Create a list
movieswith your four favorite movies. - Replace the second movie with a new one, add another movie, and display the updated list.
Lesson 2: Filtering Algorithms
-
Definition: Filtering algorithms extract elements based on specific conditions.
-
Basic Example: Filter even numbers.
numbers = [11, 14, 18, 23, 29, 32, 40] even_numbers = [num for num in numbers if num % 2 == 0] print(even_numbers) # Output: [14, 18, 32, 40]
-
Popcorn Hack #2 (8 minutes)
- Given the list
ages = [15, 20, 34, 16, 18, 21, 14, 19], write Python code to create a new list with ages 18 or older.
Lesson 3: Efficiency and Performance of Filtering Algorithms
-
Time Complexity: Filtering algorithms usually iterate through the entire list, resulting in O(n) time complexity, where
nis the number of elements. -
Considerations:
- Filtering large datasets takes longer.
- Understanding efficiency helps optimize performance in large-scale applications.
Homework Hacks
- Watch the Videos: Write 3 bullet points per video. If you have more than 6 bullet points, you’ll be rewarded.
- Create Python Code:
- Initialize a list of numbers from 1 to 30.
- Filter out and display numbers divisible by 3 but not by 5.
- Print both the original and filtered lists.
- Develop a Function:
- Write
filter_spotify_datathat reads data from a Spotify CSV and filters songs with over 10 million streams. Display the filtered data clearly.
- Write
Review Questions
- Explain what lists are in Python, including how to modify and manipulate them.
- Provide a real-world scenario where a filtering algorithm might be applied.
- Discuss why analyzing the efficiency of filtering algorithms is important for software development.
Submission
- Submit detailed code implementations and answers to the review questions by 4/10 at 11:59 PM.