Q1: List Indexing
For each of the following lists, what is the list indexing expression that evaluates to
7
? For example, ifx = [7]
, then the answer would bex[0]
. You can use the interpreter or Python Tutor to experiment with your answers. If the code would cause an error, typeError
.
>>> x = [1, 3, [5, 7], 9]
x[2][1]
>>> x = [[7]]
x[0][0]
>>> x = [3, 2, 1, [9, 8, 7]]
x[3][2]
>>> x = [[3, [5, 7], 9]]
x[0][1][1]
What would Python display? If you get stuck, try it out in the Python interpreter!
>>> lst = [3, 2, 7, [84, 83, 82]]
>>> lst[4]
Error
>>> lst[3][0]
84
Q2: Skip Add
Write a function
skip_add
that takes a single argumentn
and computes the sum of every other integer between0
andn
. Assumen
is non-negative.
this_file = __file__
def skip_add(n):
""" Takes a number n and returns n + n-2 + n-4 + n-6 + ... + 0.
>>> skip_add(5) # 5 + 3 + 1 + 0
9
>>> skip_add(10) # 10 + 8 + 6 + 4 + 2 + 0
30
>>> # Do not use while/for loops!
>>> from construct_check import check
>>> # ban i