3.10B HACKS
Student led teaching on Abstraction. Teaching how various data types can use abstraction for computational efficiency.
python popcorn hack 1;
# Creating an array (list) of numbers
my_list = [1, 2, 3, 4, 5]
# Reversing the list using the reverse() method
my_list.reverse()
# Printing the reversed list
print("Reversed list:", my_list)
Reversed list: [5, 4, 3, 2, 1]
popcorn hack 2;
# Initial array (list)
my_list = [2, 3, 4, 5]
# Using the spread operator (via list concatenation) to simulate unshift
new_item = 1
my_list = [new_item] + my_list
# Printing the updated list
print("Updated list:", my_list)
Updated list: [1, 2, 3, 4, 5]
popcorn hack 3;
# Initial list
my_list = [2, 3, 4]
# Simulate unshift() using slicing
my_list = [1, *my_list]
print("List after unshift:", my_list)
List after unshift: [1, 2, 3, 4]
pop hack 4;
# Initial array
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Use filter() to get only even numbers
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print("Even numbers:", even_numbers)
Even numbers: [2, 4, 6, 8, 10]
popcorn hack 5;
# Initial list
my_list = ['apple', 'banana', 'orange']
# Insert values using negative indexes
my_list.insert(-1, 'grape')
print("List after insertion:", my_list)
List after insertion: ['apple', 'banana', 'grape', 'orange']
popcorn hack 6;
# Two initial lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# Extend list1 with list2
list1.extend(list2)
print("Extended list:", list1)
Extended list: [1, 2, 3, 4, 5, 6]
Main Hack 1;
# Initial list
my_list = ['apple', 'banana', 'orange', 'grape', 'pear']
# Remove using remove()
my_list.remove('banana')
# Remove using pop()
my_list.pop(2)
# Remove using del
del my_list[0]
print("List after removing three items:", my_list)
List after removing three items: ['orange', 'pear']
main hack 2;
# List of numbers
numbers = [10, 15, 20, 25, 30, 35]
# Filter even numbers
even_numbers = [x for x in numbers if x % 2 == 0]
print("Even numbers:", even_numbers)
Even numbers: [10, 20, 30]
main hack 3;
# List of student grades
grades = []
def add_grade(grade):
grades.append(grade)
def remove_grade(grade):
if grade in grades:
grades.remove(grade)
else:
print("Grade not found")
def view_grades():
print("Student grades:", grades)
# Example usage
add_grade(85)
add_grade(90)
view_grades()
remove_grade(85)
view_grades()
Student grades: [85, 90]
Student grades: [90]
main hack 4;
# List of numbers
numbers = [10, 20, 30, 40, 50]
# Basic operations
print("Sum of numbers:", sum(numbers))
print("Maximum number:", max(numbers))
print("Minimum number:", min(numbers))
Sum of numbers: 150
Maximum number: 50
Minimum number: 10
main hack 5;
let myArray = [1, 2, 3, 4, 5];
console.log("Array:", myArray);
main hack 6;
myArray.forEach(function(value) {
console.log("Value:", value);
});
main hack 7;
myArray.push(6); // Add value
console.log("Array after adding 6:", myArray);
myArray.pop(); // Remove last value
console.log("Array after removing last element:", myArray);
main hack 8;
# List of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
# Count even numbers
even_count = sum(1 for num in numbers if num % 2 == 0)
print("Count of even numbers:", even_count)
Count of even numbers: 4