Year 9 · Autumn 1

Python II: lists and strings · Lesson 1 of 5

Lists: creating and indexing

PracticalTopic 14 Programming further

Year 9 · Python II: lists and strings · Lesson 1 of 5

You will learn to

  • Create a Python list with square brackets and print one item using its index.
  • Explain why the first item is at index 0 and the last is at len(list) - 1.
  • Use a negative index, like -1, to get the last item.
  • Explain the error that an index past the end of the list gives.

Year 9 · Python II: lists and strings · Lesson 1 of 5

Success criteria

You have got it when you can say:

  • I can print the first and the last item of my shopping list.
  • I can say which index holds the 4th item in a list.
  • I can explain 'list index out of range' in my own words.

Year 9 · Python II: lists and strings · Lesson 1 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last year

    A microphone hearing your voice is…

    A OutputB InputC Process

    B. Input

  2. Last year

    A AND B, with A = 1 and B = 0, gives…

    A 1B 0

    B. 0

  3. Last year

    In https://www.bbc.co.uk/news, the path is…

    A /newsB https://C www.bbc.co.uk

    A. /news

Year 9 · Python II: lists and strings · Lesson 1 of 5

Key words (1 of 2)

list
One name holding many values in order. Also called an array.
index
An item's position in a list, counting from 0.
Subroutine
A named block of code you can call when needed.
Function
A subroutine that returns a value.

Year 9 · Python II: lists and strings · Lesson 1 of 5

Key words (2 of 2)

Parameter
A placeholder in a subroutine for a value passed in.
Validation
Checking input is reasonable before using it.
Boundary data
Test data at the very edge of what's allowed.

The explanation · step 1 of 1 · part 1 of 2

Lists

A list (or array) stores many values under one name, in order.

scores = [12, 45, 33, 50]
Index0123
Value12453350
?Why?

Why start at 0? The index means 'how many steps from the start'. The first item is 0 steps away.

The explanation · step 1 of 1 · part 2 of 2

Lists (continued)

  • scores[0] → 12
  • scores[-1] → 50 (the last one)
  • scores.append(28) adds to the end
  • len(scores) → how many items
  • scores[10] → error: no item there

Year 9 · Python II: lists and strings · Lesson 1 of 5

Quick quiz · 1 of 3

In ["a", "b", "c"], the index of "c" is…

  1. A 3
  2. B c
  3. C 2

C. 2

Counting starts at 0: a = 0, b = 1, c = 2.

Year 9 · Python II: lists and strings · Lesson 1 of 5

Quick quiz · 2 of 3

word = "Python". What is word[1:4]?

  1. A "yth"
  2. B "Pyth"
  3. C "ytho"

A. "yth"

Start at index 1 (y), stop before index 4 (o): y, t, h. That's 4 − 1 = 3 characters.

Year 9 · Python II: lists and strings · Lesson 1 of 5

Quick quiz · 3 of 3

In def area(w, h): … and a = area(4, 3), which are the arguments?

  1. A area and a
  2. B 4 and 3
  3. C w and h

B. 4 and 3

Arguments are the actual values passed in the call. w and h are parameters.

Year 9 · Python II: lists and strings · Lesson 1 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. A variable made inside a function is…

    A Global: usable anywhereB Local: only exists inside it while it runsC Saved to a file

    B. Local: only exists inside it while it runs Local variables are wiped when the function returns.

  2. Testing a 0–50 range with 50 is…

    A Erroneous dataB Normal dataC Boundary data

    C. Boundary data It's right at the edge of what's allowed.