Why a map has no trees
Look at a train map. It shows stations and lines. It does not show streets, trees or shops.
The map is useful because it leaves things out. If it showed everything, you could not read it.
Programmers do the same with a problem. Abstraction means keeping the details that matter and leaving out the rest.
Key words
| Word | What it means | Example |
|---|---|---|
| Decomposition | Breaking a big problem into smaller parts. | a shop: basket, payment |
| Pattern recognition | Finding what repeats or looks alike. | two apps need a login |
| Abstraction | Keeping the details that matter and leaving out the rest. | a train map |
| Input | Data that goes into a solution. | age of a passenger |
| Output | Data that comes out of a solution. | the fare to pay |
| Constant | A value that never changes. | the child fare |
| Variable | A name that holds a value. The value can change. | the age of a passenger |
| Outcome | The result we want at the end. | the passenger pays the right fare |
The cells students fill in are left blank in this preview.
Keep it or drop it?
Have a go
Keep it or drop it?
3 questionsAbstraction helps you focus
Abstraction helps you focus. A real problem has hundreds of details. Most do not matter for your solution.
It also makes a solution easy to reuse. A solution that ignores small details works for many cases.
The three tasks of abstraction
| Task | What you do | Bus ticket app |
|---|---|---|
| 1 Find what you need | List the information the solution must have. | Age, route, number of journeys |
| 2 Filter out the rest | Drop details that do not change the answer. | Bus colour, bus logo |
| 3 Hide how it works inside | Let people use a part without seeing how it works. | You press Buy. You do not see the payment code. |
The cells students fill in are left blank in this preview.
Try it: hide the details
price = 2.4567
print(round(price, 2))
# round() hides the maths inside. You only say what you want.
# Change 2 to 1 and run it again.
Make a free account to save your work and track your progress.
Six questions to ask
To use abstraction on a problem, answer six questions. Look at the table. It uses a bus fare calculator.
Abstraction for a bus fare calculator
| Question | Answer for the bus fare calculator |
|---|---|
| What inputs do we need? | The age of the passenger. The number of journeys. |
| What outputs and outcomes do we expect? | The cost to pay. The outcome: the passenger pays the right amount. |
| What will vary? | The age and the number of journeys change for each passenger. |
| What will stay constant? | The child fare is 1.00. The adult fare is 2.00. |
| What are the key actions? | Choose the right fare. Multiply it by the number of journeys (fare x journeys). |
| What repeats? | The same sum for every journey and every passenger. |
The cells students fill in are left blank in this preview.
Watch: keep what matters
Press Play on the short film below. It turns a busy town into a train map, then shows the three tasks of abstraction and the bus fare calculator from the table. Watch what goes wrong when a detail that matters is left out.
Film: abstraction
Abstraction: keep what matters
Every scene, as text
- Abstraction means keeping the details that matter and leaving out the rest. A town from above has two train lines and their stations, and its streets, trees and shops as well: drawn all together, the lines are hard to read. A train map leaves the streets, trees and shops out and keeps only the stations and the lines.
- The three tasks of abstraction: a bus ticket app. 1 Find what you need: age, route, number of journeys. 2 Filter out the rest: the bus colour and the bus logo do not change the answer, so they are struck through. 3 Hide how it works inside: you press Buy, and you do not see the payment code.
- Code hides how it works inside too. price = 2.4567, then print(round(price, 2)). round() hides the maths inside: you only say what you want, 2 decimal places. It prints 2.46.
- Myth (false): abstraction means make it vague. The bus fare calculator with the age left out, struck through: everyone pays the adult fare, 2.00. A child aged 12 making 3 journeys pays 2.00 x 3 = 6.00. That is wrong, marked with an X: a child pays the child fare. Leaving out a detail that matters is a mistake.
- The cure: choose the right detail. The inputs, age 12 and 3 journeys, vary for each passenger. The constants never change: the child fare is 1.00 and the adult fare is 2.00. The key action: 12 is under 16, so the child fare, times the journeys: 1.00 x 3. The output, 3.00, is the right fare.
Match the question to the answer
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Your turn: the fare calculator
Use abstraction to build a bus fare calculator. The child fare is 1.0. The adult fare is 2.0. A child is under 16.
- Set the two constants first.
- fare_for_one gives the fare for one journey.
- trip_cost multiplies the fare by the number of journeys.
- describe_trip gives text like 3 journeys cost 6.0.
Press Run Tests to check.
CHILD_FARE = 0
ADULT_FARE = 0
def fare_for_one(age):
# Under 16 pays the child fare. Everyone else pays the adult fare.
return 0
def trip_cost(age, journeys):
# The cost of several journeys for one person.
return 0
def describe_trip(age, journeys):
# Text like: 3 journeys cost 6.0
return ""
Make a free account to save your work, or press Start lab to work through the whole lesson.
The four parts work as one
The four parts are not four separate jobs. Decomposition gives you small parts. In each part, you look for patterns and use abstraction. Then you write the steps.
You must judge which part suits each problem. This is the skill the exam tests.
Which part suits which problem?
| Problem | Best part to start with | Why |
|---|---|---|
| A huge app with many screens | Decomposition | It is too big to see all at once. |
| Two new tasks that look like old ones | Pattern recognition | You may reuse an old solution. |
| A map with too much detail | Abstraction | Hide what does not matter. |
| A job that must be repeated 1000 times | Algorithmic design (writing clear steps) | Clear steps let a computer repeat it. |
The cells students fill in are left blank in this preview.
Judge the part
Have a go
Judge the part
3 questionsYour turn: abstraction sheet
Choose one: a cinema booking app, a school canteen checkout, or a football score board.
Answer the six questions for it.
- What inputs do we need?
- What are the outputs and the outcome?
- What varies?
- What stays constant?
- What are the key actions?
- What repeats?
Then list two details you leave out. Say why you leave them out.
A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go
Exit quiz
6 questionsRemember
Important
Abstraction keeps what matters and hides the rest. Name the inputs, outputs, what varies, what is constant, the key actions and what repeats.