Lists and functions
Answers are for parents and teachers.
1
scores = [3, 9, 4]. Write the result of scores[0], scores[2], len(scores) and of scores[-1].
3, 4, 3 and 4.
2
Trace this program in a table and write what it prints.
Python
1scores = [3, 9, 4] 2best = scores[0] 3for x in scores: 4 if x > best: 5 best = x 6print(best)
9. best starts at 3, then 3 stays (3 > 3 is False), 9 replaces it, 4 does not.
3
Write a function cube(n) that returns n * n * n, and write the value of cube(3).
def cube(n): return n * n * n (on two lines, with the return indented). cube(3) is 27.
4
Write what this program prints, and explain why the second line shows None.
Python
1def show(n): 2 print(n * n) 3 4r = show(5) 5print(r)
It prints 25 and then None: show prints the square on the screen but returns nothing, so r holds None.
5
In the passed program, change the pass mark to 60. How many of [45, 72, 50, 38, 90] pass now?
2 (72 and 90).