AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Term 1 in review

Week 11 · Review: Term 1 · 3 lessons
2

Words and lists

Computing 9: Topics 1.1–1.7, pp. 11–134
GoalReview Python loops, strings and lists: predict output, find errors, and write short programs with for loops.
New words
index · the position of an item in a list or a character in a string; counting starts at 0list · one variable that holds many items in order; written with square bracketsmethod · a built-in action attached to a value, such as .upper() or .append()
Explanation

A for loop with range(stop) repeats the body stop times, and the counter starts at 0. With range(start, stop) the counter begins at start and stops before stop. A string and a list both have an index from 0, and len() gives how many characters or items there are. The last index is len() − 1, and using any larger index causes an IndexError. To go through a list you can write for item in my_list. To go through a string by position, use for i in range(len(word)). Methods such as .lower() give back a new value, and they do not change the original unless you store the result. A running total, a counter and a best-so-far variable are the usual patterns for loops.

ValueExpressionResult
marks = [14, 9, 20]marks[0]14
marks = [14, 9, 20]len(marks)3
word = "Moon"word[3]n
word = "Moon"word.upper()MOON
Worked examples
Python
1marks = [14, 9, 20, 17, 11]
2best = marks[0]
3for m in marks:
4    if m > best:
5        best = m
6print(best)
Output
20
Finding the best of a list. best starts as the first mark. The loop replaces best whenever a mark is bigger. The marks 14, 9, 20, 17 and 11 give a best of 20.
Python
1text = input("Text? ").lower()
2count = 0
3for i in range(len(text)):
4    if text[i] == "e":
5        count = count + 1
6print(count)
Output
5
Counting letters whatever the case. The user types Eleven Elves. The text is changed to lowercase first, so E and e both match. The letter e appears three times in eleven and twice in elves, so the program prints 5.
Python
1days = ["Mon", "Tue", "Wed"]
2print(days[3])
Output
IndexError: list index out of range
A run-time error. The list has three items, so the valid indexes are 0, 1 and 2. Index 3 does not exist, so Python stops and prints the message.
Class activity

List theatre (no computer). Write six words on six cards and put them in a row, numbered 0 to 5. Your partner calls instructions such as ‘show me item 4’, ‘change item 2 to bird’, ‘how long is the list?’, ‘add the word kite at the end’ and ‘show me item 6’. Say what Python would do for each one. For ‘item 6’, say which error appears. Then do the same with a word like planet, where each card is one letter.

Practice
1
What does this program print?
Python
1nums = [3, 8, 5]
2total = 0
3for n in nums:
4    total = total + n * 2
5print(total)
2
The program should print every letter of level, but it stops with an error after printing five letters. Give the last line of the message and correct the program.
Python
1word = "level"
2for i in range(len(word) + 1):
3    print(word[i])
3
Write a program that stores the list [12, 5, 18, 7, 3, 9] and prints how many of the numbers are below 10.
4
The variable is s = "Orange". Complete the table.
ExpressionValue
s[0]?
len(s)?
s.upper()?
s[len(s) - 1]?
s.lower()[1]?