Skip to the content.

3.10 Hacks

hacks

Popcorn Hack 1

my_list = [1, 2, 3, 4, 5]

print("Original list:", my_list)

my_list.append(6)
print("Updated list:", my_list)

Original list: [1, 2, 3, 4, 5]
Updated list: [1, 2, 3, 4, 5, 6]

Popcorn Hack 2

alist = [1, 2, 3, 4, 5]
print("Original list:", alist)

alist.insert(5, 6) 
print("List after insertion:", alist)

alist.remove(1)
print(alist)
Original list: [1, 2, 3, 4, 5]
List after insertion: [1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 6]

Popcorn Hack 3

aList = ['Hi', 'Hello', 'Cya']
aList[1] = 'Bye'  # Change 'banana' to 'kiwi'
print(aList)
['Hi', 'Bye', 'Cya']

HW Hack

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

even_sum = 0
for num in nums:
    if num % 2 == 0: 
        even_sum += num


min_value = min(nums)
max_value = max(nums)

print("Sum of even numbers in the list:", even_sum)
print("Minimum value in the list:", min_value)
print("Maximum value in the list:", max_value)

Sum of even numbers in the list: 30
Minimum value in the list: 1
Maximum value in the list: 10