Lists, functions and testing
Real programs are split into small named pieces called functions, and store groups of data in lists. Then we test them properly.
Part of your national curriculum
- Algorithms and programming: Use a variety of data structures, including lists, tables or arrays, and design modular programs that use procedures or functions
- Algorithms and programming: Design and develop modular programs that use procedures or functions, and test and correct them systematically
Lesson overview
What you'll learn in this lesson
Design and develop modular programs that use procedures or functions, and test and correct them systematically
Key learning points
- • Lists
- • Why split code up
- • Functions
- • Testing systematically
This lesson at a glance
- 20 minutes
- 18 parts to scroll through
- 4 quick checks
- Marked quiz at the end
- Gentle pace: short sittings with pauses
Words to know
Scroll down — the lesson carries on below
Introduction
Part 1 of 18
Visual introductionPicture this
Lists, functions and testing
Real programs are split into small named pieces called functions, and store groups of data in lists. Then we test them properly.
In a nutshell
Design and develop modular programs that use procedures or functions, and test and correct them systematically
What you already know
Part 2 of 18
Before we start
What you already know
You can write if statements and loops in Python.
Key words
Part 3 of 18
Key words
Words you'll need today
- list
- An ordered collection, like animals = ["lynx", "otter", "owl"].
- index
- An item's position in a list. The first item is at index 0.
- function
- A named, reusable block of code, made with def.
- test data
- Values chosen to check a program works, including normal, boundary and wrong values.
Learning cycle
Part 4 of 18
Learning cycle 1 of 2
Part 1: Lists
A short piece of teaching, then a check to make sure it has landed.
Explore the idea
Part 5 of 18
Learn
Lists
animals = ["lynx", "otter", "owl"]. animals[0] is "lynx". animals.append("wolf") adds one. for a in animals: print(a) shows each one.
Explore the idea
Part 6 of 18
Learn
Why split code up
Breaking a big problem into smaller parts is called decomposition. Each part can be written, tested and fixed on its own.
Quick check
Part 7 of 18
Quick check
In ["lynx", "otter", "owl"], what is at index 1?
Reset break
Part 8 of 18
Pause
That's sitting 1 of 2 done
Stretch, get a drink, look out of the window. There is no timer and nothing is counting down — your place is saved, so you can come back in five minutes or tomorrow.
Quick check
Part 9 of 18
Quick check
Splitting a problem into smaller parts is called:
Learning cycle
Part 10 of 18
Learning cycle 2 of 2
Part 2: Functions and testing
A short piece of teaching, then a check to make sure it has landed.
Explore the idea
Part 11 of 18
Learn
Functions
def greet(name): print("Hello", name) greet("Alex") runs it. Write it once, use it many times. Change it in one place and every use updates.
Explore the idea
Part 12 of 18
Learn
Testing systematically
For a pass mark of 50, test normal data (70), boundary data (50 and 49) and wrong data ("fish"). Write what you expect before you run each test.
Quick check
Part 13 of 18
Quick check
Which word creates a function in Python?
Quick check
Part 14 of 18
Quick check
For a pass mark of 50, which is boundary test data?
Common mix-ups
Part 15 of 18
Common mix-ups
Things people often get wrong
- The first item in a list is index 0, not 1.
- Defining a function doesn't run it. You have to call it by name.
Challenge round
Part 16 of 18
Game · Sort it
Which of these are true?
Drag each card into the right column. Tap a card first if dragging is fiddly.
True
Not true
Mastery quiz
Part 17 of 18
Marked quiz
End of lesson quiz: Lists, functions and testing
4 questions, marked with the reasoning shown. No timer.
1. What does animals.append("wolf") do?
2. Why use functions?
3. Before running a test you should:
4. Why use a list rather than 30 separate variables for 30 test scores?
Lesson round-up
Part 18 of 18
Lesson round-up
Ready when you are
Quiz score
Not sat
Games
Not played
Points this lesson
0
Best run
0 in a row
Luna: 0 out of 4 on the practice checks. Only if you feel up to it — one more?
Ask LunaDo the work
Your written work for this lesson
This is the part you'd do in your book at school. Pick the pieces below, write here or on paper, and photograph anything you draw or work out.
How a supermarket checkout knows the price
When a barcode is scanned, the checkout does not store the price itself. It sends the barcode number to a database and reads back a record: product name, price, whether it needs an age check. The record is the single place the price lives, so changing one row changes every till in the shop at once.
Problems come from bad data, not bad scanners. If two products share a barcode, or a price update reaches the shop overnight but the till holds an old copy in its cache, customers are charged the wrong amount. Shops check this with test transactions: scan a known item, expect a known price, record what actually happened.
Good systems also log every scan. If a customer complains, the log shows exactly which record was used and when.
Written for Good Learning Co.
Reading questions
- Write in your own words what the checkout sends and what it receives back.
- Explain one way bad data could cause a wrong price, and how logging helps find it.
- Design a test table with four tests for a new checkout, with expected and actual results.
Reading and notes
ReadingRead the lesson text again and note the key terms and how each one is used, in your own words — as you would for revision.
Half a page of notes
Extended writing
WritingExplain a real system that uses this idea — how it works, where it can go wrong, and how you would make it safer or better.
250–350 words
Algorithm on paper
DiagramWrite your steps out as pseudocode or a flowchart before you type anything, then photograph it.
1 flowchart or 10 lines
Code write-up
WritingCopy your code in, then explain underneath what each part does and what you had to fix.
Code + 5 sentences
Test table
Working outMake a test table: what you tried, what you expected, what actually happened.
4–6 tests
Saved as you type.
Part 1 of 18 · Introduction
Playground
Python Lab
Write and run real Python
Pick a challenge, read it, predict what it will print, then press Run. Change something and run again.
How this is going
- Not looked at yet
Use a variety of data structures, including lists, tables or arrays, and design modular programs that use procedures or functions
- Not looked at yet
Design and develop modular programs that use procedures or functions, and test and correct them systematically
