1.
a. Works fine. x gets "banana" and y gets "12"
b. Error. To unpack a tuple, you have to have the same number of variables on the left as in the tuple on the right.
c. Works fine. This is the same result as a., but just using t to store the tuple temporarily.
d. Works fine and prints "pie". Tuples are indexed just like lists.
e. Error. Same issues as b., now with too few variables on the left.
f. Error. Unlike lists, tuples are immutable, i.e. you cannot change their values.
2.
def my_max(numbers):
max = numbers[0]
for num in numbers:
if num > max:
max = num
return max
They only difference here is what we initialize max to. In the version in class, since we knew they
were positive, we could pick -1 since we knew that would be smaller than any of the numbers in the list.
Here, we picked the first element in the list to get things started. If it's the max, then max will
stay at that value. If it's not the max, then as we iterate through the list some other element will
replace it eventually and we'll get the correct answer.