Popcorn Hack 1

# Create an initial list with some numbers
my_list = [10, 20, 30, 40, 50]

# Insert a value at the second-to-last position
my_list.insert(-1, 35)
print("After inserting 35 at second-to-last position:", my_list)

# Insert a value at the third-to-last position
my_list.insert(-2, 25)
print("After inserting 25 at third-to-last position:", my_list)

# Insert a value at the last position
my_list.insert(-1, 45)
print("After inserting 45 at last position:", my_list)
After inserting 35 at second-to-last position: [10, 20, 30, 40, 35, 50]
After inserting 25 at third-to-last position: [10, 20, 30, 40, 25, 35, 50]
After inserting 45 at last position: [10, 20, 30, 40, 25, 35, 45, 50]

Popcorn Hack 2

# Create the first list with some numbers
list1 = [1, 2, 3, 4, 5]

# Create the second list with some numbers
list2 = [6, 7, 8, 9, 10]

# Add the second list to the first list using the extend() method
list1.extend(list2)

# Print the combined list
print("Combined list:", list1)
Combined list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Popcorn Hack 3

fruits = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"]

fruits.remove("banana")
fruits.pop(3)
del fruits[1]

print(fruits)
['apple', 'date', 'fig', 'grape']

Popcorn Hack 4

%%js 

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array.reverse();

console.log(array);
<IPython.core.display.Javascript object>

Popcorn Hack 5

%%js 

// Create an initial array
let fruits = ['apple', 'banana', 'cherry'];

// Add elements to the beginning of the array using unshift()
fruits.unshift('orange', 'grape');

// Print the updated array
console.log(fruits); // Output: ['orange', 'grape', 'apple', 'banana', 'cherry']

// Create an initial array
let vegetables = ['carrot', 'broccoli', 'spinach'];

// Add elements to the beginning of the array using the spread operator
let moreVegetables = ['potato', 'tomato', ...vegetables];

// Print the updated array
console.log(moreVegetables); // Output: ['potato', 'tomato', 'carrot', 'broccoli', 'spinach']

<IPython.core.display.Javascript object>

Popcorn Hack 6

%%js 

var carBrands = ['Toyota', 'Ford', 'BMW', 'Mercedes', 'Audi', 'Chevrolet', 'Honda'];

var longBrandCount = carBrands.filter(brand => brand.length > 4).length; 

console.log(longBrandCount);
<IPython.core.display.Javascript object>

Homework Hacks

Hack #1 Grocery List

# Create an empty list to store grocery items
grocery_list = []

# Input three grocery items and add them to the list
grocery_list.append(input("Enter first grocery item: "))
grocery_list.append(input("Enter second grocery item: "))
grocery_list.append(input("Enter third grocery item: "))

# Display the current grocery list after all items are added
print("Current grocery list:", grocery_list)

# Sort the list alphabetically and print the sorted list
grocery_list.sort()

# Remove one item specified by them and display the updated list
grocery_list.remove(input("Enter an item to remove: "))

print("Updated grocery list:", grocery_list)
Current grocery list: ['apple', 'flour', 'milk']
Updated grocery list: ['apple', 'milk']

Hack #2 Filtering Even Numbers

# Create a list of integers from 1 to 20
original_list = list(range(1, 21))

# Print the original list
print("Original list:", original_list)

# Create a new list that contains only the even numbers from the original list
even_numbers = [number for number in original_list if number % 2 == 0]

# Print the list of even numbers
print("Even numbers:", even_numbers)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Even numbers: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Hack #3 Student Grades

# Create an empty list to store student grades
grades = []

# Input three grades and add them to the list
grades.append(int(input("Enter first grade: ")))
grades.append(int(input("Enter second grade: ")))
grades.append(int(input("Enter third grade: ")))

# Display the list of grades after all grades are entered
print("Grades:", grades)

# Create a new list that contains only grades above 60
passing_grades = [grade for grade in grades if grade > 60]

# Display the list of passing grades
print("Passing grades:", passing_grades)
Grades: [40, 76, 82]
Passing grades: [76, 82]

Hack #4 List Ordering

# Create a list of numbers from 1 to 10
numbers = list(range(1, 11))

# Print the original list
print("Original list:", numbers)

# Sort the list in descending order
numbers.sort(reverse=True)

# Slice the list to get the first five numbers
first_five = numbers[:5]

# Print the first five numbers
print("First five numbers:", first_five)

# Sort the list in ascending order
numbers.sort()

# Print the sorted list
print("Sorted list:", numbers)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
First five numbers: [10, 9, 8, 7, 6]
Sorted list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Hack #5 Making an Array

%%js 

// Create an array with at least 5 values
let myArray = ['apple', 'banana', 'cherry', 'date', 'fig'];

// Display the array using console.log()
console.log("Original array:", myArray);

// Reverse the array using the reverse() method
myArray.reverse();

// Display the reversed array using console.log()
console.log("Reversed array:", myArray);
<IPython.core.display.Javascript object>

Hack #6 Indexing

%%js 

// Define the array
let sports = ["soccer", "football", "basketball", "wrestling", "swimming"];

// Display the values "soccer" and "wrestling" using their indexes
console.log(sports[0]); // Output: soccer
console.log(sports[3]); // Output: wrestling
<IPython.core.display.Javascript object>

Hack #7 Student Grades

%%js

// Initialize the array with four items
let choresList = ['dishes', 'laundry', 'vacuum', 'trash'];

// Display the initial list
console.log("Initial list:", choresList);

// Use push() to add an item to the end of the list
choresList.push('mop');
console.log("After push('mop'):", choresList);

// Use shift() to remove the first item from the list
choresList.shift();
console.log("After shift():", choresList);

// Use pop() to remove the last item from the list
choresList.pop();
console.log("After pop():", choresList);

// Use unshift() to add an item to the beginning of the list
choresList.unshift('groceries');
console.log("After unshift('groceries'):", choresList);

// Bonus: Use push() and the spread operator to add multiple values at once
choresList.push(...['windows', 'bathroom']);
console.log("After push(...['windows', 'bathroom']):", choresList);
<IPython.core.display.Javascript object>

Hack #8 Student Grades

%%js

// Create an array with ten random numbers (both even and odd)
let randomNumbers = [12, 7, 22, 35, 44, 19, 28, 31, 50, 13];

// Function to count the number of even numbers in the array
function countEvenNumbers(arr) {
    let evenCount = 0;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] % 2 === 0) {
            evenCount++;
        }
    }
    return evenCount;
}

// Call the function and display the output using console.log()
let evenNumberCount = countEvenNumbers(randomNumbers);
console.log("Number of even numbers in the array:", evenNumberCount);

<IPython.core.display.Javascript object>