Skip to the content.
Home Accounts Setup Verify Play Hacks

Gaheera Babbar P4 Popcorn Hacks 3.10B

3.10B Lesson Popcorn and Homework Hacks

Python

#Python Popcorn Hack 1
# Create an array (list in Python)
my_array = [1, 2, 3, 4, 5]
print("Original array:", my_array)

# Reverse the array
my_array.reverse()
print("Reversed array:", my_array)


#Python Popcorn Hack 2
# Create an array
my_array = [3, 4, 5]

# Use list concatenation to mimic the unshift effect
new_element = [2]  # The element to add
my_array = new_element + my_array
print("Array after unshift:", my_array)

#Python Popcorn Hack 3
# Create an array with numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Use filter to create a new array with only even numbers
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print("Even numbers using filter:", even_numbers)

#Python Popcorn Hack 4
# Create a list
my_list = [1, 2, 3]

# Insert values at negative indexes
my_list.insert(-1, 1.5)  # Inserts 1.5 before the last element
my_list.insert(-2, 1.2)  # Inserts 1.2 before the second last element
print("List after inserting with negative indexes:", my_list)

#Python Popcorn Hack 5
# Create two lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]

# Use extend to add list2 to list1
list1.extend(list2)
print("Combined list using extend:", list1)

#Python Popcorn Hack 6
# Create a list
my_list = ['apple', 'banana', 'cherry', 'strawberry', 'grape']

# Remove an item using remove()
my_list.remove('banana')
print("List after removing 'banana':", my_list)

# Remove an item using pop()
my_list.pop(2)  # Removes 'cherry'
print("List after popping index 2:", my_list)

# Remove an item using del
del my_list[0]  # Removes 'apple'
print("List after deleting index 0:", my_list)

#Python HW Hack 1: Grocery List
# Step 1: Create an empty list to store grocery items
grocery_list = []

# Step 2: Input three grocery items and add them to the list
for i in range(3):
    item = input(f"Enter grocery item {i+1}: ")
    grocery_list.append(item)

# Step 3: Display the current grocery list after all items are added
print("\nCurrent grocery list:", grocery_list)

# Step 4: Sort the list alphabetically and print the sorted list
grocery_list.sort()
print("\nSorted grocery list:", grocery_list)

# Step 5: Remove one item specified by the user and display the updated list
item_to_remove = input("\nEnter an item to remove from the list: ")
if item_to_remove in grocery_list:
    grocery_list.remove(item_to_remove)
    print("\nUpdated grocery list:", grocery_list)
else:
    print(f"\nItem '{item_to_remove}' not found in the list.")
Current grocery list: ['apples', 'bananas', 'bread']

Sorted grocery list: ['apples', 'bananas', 'bread']

Updated grocery list: ['apples', 'bread']
#Python HW Hack 2: Filtering Even Numbers
# Step 1: Create a list of integers from 1 to 20
original_list = list(range(1, 21))

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

# Step 3: Create a new list with only the even numbers using list comprehension
even_numbers = [num for num in original_list if num % 2 == 0]

# Step 4: Print the list of even numbers
print("Even numbers:", even_numbers)

#Python HW Hack 3: Student Grades
# Step 1: Create an empty list to store student grades
grades = []

# Step 2: Input three grades (as integers) and add them to the list
for i in range(3):
    grade = int(input(f"Enter grade {i+1}: "))
    grades.append(grade)

# Step 3: Print the list of grades after all grades are entered
print("\nList of grades:", grades)

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

# Print this list of passing grades
print("Grades above 60:", passing_grades)

List of grades: [67, 85, 54]
Grades above 60: [67, 85]
#Python HW Hack 4: Basic Operations
# Step 1: Create a list of numbers from 1 to 10
numbers = list(range(1, 11))

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

# Step 3: Sort the list in descending order
numbers.sort(reverse=True)
print("List in descending order:", numbers)

# Step 4: Slice the list to get the first five numbers and print them
first_five = numbers[:5]
print("First five numbers:", first_five)

# Step 5: Sort the list again in ascending order and print it
numbers.sort()
print("List in ascending order:", numbers)

%%js
//JS HW Hack 1: Creating an Array

let Array = [1, 2, 3, 4, 5];

// Step 2: Display the array using console.log()
console.log("Original array:", Array);

// Bonus: Reverse the array using the reverse() method
Array.reverse();

// Step 3: Display the reversed array
console.log("Reversed array:", Array);


%%js
// JS HW Hack 2: Accessing Elements
// Given array
let sports = ["soccer", "football", "basketball", "wrestling", "swimming"];

// Accessing and displaying values using their indexes
console.log(sports[0]); // Output: soccer
console.log(sports[3]); // Output: wrestling

<IPython.core.display.Javascript object>
%%js
// JS HW Hack 3: Adding and Removing Items

let choresList = ["laundry", "dishes", "vacuuming", "grocery shopping"];
console.log("Initial chores list:", choresList);

// Step 2: Use push() to add an item
choresList.push("cleaning the bathroom");
console.log("After push:", choresList); // Output: ["laundry", "dishes", "vacuuming", "grocery shopping", "cleaning the bathroom"]

// Step 3: Use shift() to remove the first item
choresList.shift();
console.log("After shift:", choresList); // Output: ["dishes", "vacuuming", "grocery shopping", "cleaning the bathroom"]

// Step 4: Use pop() to remove the last item
choresList.pop();
console.log("After pop:", choresList); // Output: ["dishes", "vacuuming", "grocery shopping"]

// Step 5: Use unshift() to add an item to the beginning
choresList.unshift("mopping");
console.log("After unshift:", choresList); // Output: ["mopping", "dishes", "vacuuming", "grocery shopping"]

// Bonus: Use push() and spread operator to add multiple values
choresList.push(...["taking out trash", "dusting", "organizing"]);
console.log("After adding multiple chores:", choresList); 
// Output: ["mopping", "dishes", "vacuuming", "grocery shopping", "taking out trash", "dusting", "organizing"]

%%js
// JS HW Hack 4: Iterations and Conditionals

let randomNumbers = [12, 7, 19, 24, 35, 8, 42, 5, 14, 27];
console.log("Random Numbers:", randomNumbers);

// Step 2: Function to count even numbers
function countEvenNumbers(arr) {
    let count = 0; // Initialize count variable
    for (let number of arr) { // Iterate through the array
        if (number % 2 === 0) { // Check if the number is even
            count++; // Increment the count if even
        }
    }
    return count; // Return the total count of even numbers
}

// Step 3: Call the function and display the output
let evenCount = countEvenNumbers(randomNumbers);
console.log("Count of even numbers:", evenCount);