A brief note on Lists and Functions: making, reading and changing lists, lists with loops, copies and tables, functions with parameters and return, testing a function, and fixing your own programs. With a worked example, practice, a program and questions to check yourself.
A brief note on Lists and Functions. Read it before a test. Try each question at the end before you open its answer.
marks = [56, 72, 91]. It can hold any values, a mix, or none: [].marks[0] is 56. A negative position counts from the end, so marks[-1] is 91.a[1:3] starts at position 1 and stops before position 3. A slice is always a list.len(a) counts the items. x in a asks whether x is in the list. a.index(x) gives its position.| Method | What it does |
|---|---|
a.append(x) | adds x at the end |
a.insert(i, x) | puts x at position i |
a.remove(x) | removes the first x |
a.pop() | removes the last item and gives it back |
a.sort() | sorts the list itself |
None. So never write a = a.sort().sorted(a) makes a new sorted list, and the original stays as it was.b = a gives the same list a second name. b = a.copy() makes a real copy.table[row][col] is one cell.for item in a: goes through the items, one in each round.sum(), max(), min() and len() give quick answers.if. To build a new list, start with [] and append.for i in range(len(names)): reads both at position i.def name(parameters): and the indented lines under it define a function. Defining does not run it.name(arguments) calls it. A function must be defined before the line that calls it.return gives a value back and ends the function. print only shows. A function without return gives back None.def tax(price, rate=5):. Parameters with defaults come last.return out.PASS or FAIL. Test both sides of every edge.| Fault | What you see |
|---|---|
| syntax error | Python cannot read a line, and nothing runs |
| run-time error | the program stops with a message part of the way |
| logic fault | it runs to the end, but the answer is wrong |
What does this program show? The heights are in centimetres.
values gets the list of heights, and limit gets 140.count round by round.v | v > 140? | count |
|---|---|---|
| 132 | no | 0 |
| 145 | yes | 1 |
| 128 | no | 1 |
| 150 | yes | 2 |
| 141 | yes | 3 |
return count gives back 3, and print shows 3. Three children are taller than 140 cm.Five athletes jump on sports day. Two lists in the same order hold the names and the jumps in centimetres. Two small functions do the thinking.
a[0]. Asking for a position that does not exist stops with IndexError: list index out of range.a = a.sort(). sort() gives back None, so a is lost. Then a[0] stops with TypeError: 'NoneType' object is not subscriptable. Write a.sort() on its own line.print instead of return. The caller gets None. Then double(4) + 1 stops with TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'.def speed(distance, time):, the call speed(100) stops with TypeError: speed() missing 1 required positional argument: 'time'.b = a as a copy. Both names are the same list, so a change through b shows in a. Use a.copy().days = ["Mon", "Tue", "Wed", "Thu", "Fri"]. Which day is days[3], and which slice gives only Tue and Wed?double(n) whose only line is print(n * 2). Then he writes x = double(4) + 1. What goes wrong, and how should the function end?a = [3, 1, 2], then b = a, then b.append(5). What is in a now?is_teen(age) should give True for ages from 13 to 19 years, and False otherwise. Which ages would you test, and why?Log in to join the discussion.
Loading…