Summary | Effects | Digital Divide | Computing Bias | Crowdsourcing | Legal/Ethical Concerns | Safe Computing |
Binary Search Algorithm | Lists and Filtering Algorithm | Simulation/Games and Random Algorithms | Big O and Algorithm Efficiency | Binary Base 2 Math + Logic Gates | Color Codes/Images/Base64 | Relevancy |
Lists and Filtering Algorithm
Lists and Filtering Algorithm Notes and Homework
Popcorn Hack 1
What are some possible benefits of using lists? What are some real world examples of lists being used in code ?
Answer: Store lots of data in one place, Access each item individually, Filter, sort, and organize that data
Popcorn Hack 2
What does this code output? eraser items = [“pen”, “pencil”, “marker”, “eraser”] items.remove(“pencil”) items.append(“sharpener”) print(items[2])
Popcorn Hack 3: What are some real world examples of filtering algorithms?
Inbox → email is unread → Unread emails only (new list) Online Shopping → price < $20 → Discounted items (new list) Spotify → Artist == “Singer A” → Only songs by Singer A (new list)
# Creating a list of five favorite fruits
fruits = ["apple", "banana", "cherry", "mango", "kiwi"]
# List Procedure 1: Append a new item to the list
# This adds a new fruit to the end of the list
fruits.append("pineapple")
print("After append:", fruits)
# List Procedure 2: Remove an item from the list
# This removes "banana" from the list
fruits.remove("banana")
print("After remove:", fruits)
# List Procedure 3: Sort the list alphabetically
# This arranges the list in alphabetical order
fruits.sort()
print("After sort:", fruits)
# Traversing a list using a for loop
print("Traversing the fruits list:")
for fruit in fruits:
print("Fruit:", fruit)
# 🔍 Traversal Explanation:
# We use a `for` loop to go through each item in the 'fruits' list.
# The loop picks one fruit at a time and executes the print statement for each.
import pandas as pd
# Step 1: Start with a list of fruits with calorie values
fruit_data = {
"Fruit": ["apple", "banana", "cherry", "mango", "kiwi", "pineapple"],
"Calories": [95, 105, 50, 99, 42, 82]
}
# Convert to a pandas DataFrame
df = pd.DataFrame(fruit_data)
print("Original Data:")
print(df)
# Step 2: Apply a condition – let's say we want fruits under 100 calories
# This filters the DataFrame for only fruits with fewer than 100 calories
low_cal_fruits = df[df["Calories"] < 100]
print("\nFiltered Fruits (Calories < 100):")
print(low_cal_fruits)
Final Reflection
Filtering algorithms and lists are used in real life to sort and select specific data, like filtering low-calorie foods or finding affordable products online. They help make decisions faster by narrowing down large sets of information based on certain conditions.