Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

1.1 (4 of 4): Abstraction: keep what matters

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
DecompositionBreaking a big problem into smaller parts.a shop: basket, payment
Pattern recognitionFinding what repeats or looks alike.two apps need a login
AbstractionKeeping the details that matter and leaving out the rest.a train map
InputData that goes into a solution.age of a passenger
OutputData that comes out of a solution.the fare to pay
ConstantA value that never changes.the child fare
VariableA name that holds a value. The value can change.the age of a passenger
OutcomeThe 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Abstraction 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

TaskWhat you doBus ticket app
1 Find what you needList the information the solution must have.Age, route, number of journeys
2 Filter out the restDrop details that do not change the answer.Bus colour, bus logo
3 Hide how it works insideLet 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

Code: python
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

QuestionAnswer 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

  1. Set the two constants first.
  2. fare_for_one gives the fare for one journey.
  3. trip_cost multiplies the fare by the number of journeys.
  4. describe_trip gives text like 3 journeys cost 6.0.

Press Run Tests to check.

Code: python
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?

ProblemBest part to start withWhy
A huge app with many screensDecompositionIt is too big to see all at once.
Two new tasks that look like old onesPattern recognitionYou may reuse an old solution.
A map with too much detailAbstractionHide what does not matter.
A job that must be repeated 1000 timesAlgorithmic 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your turn: abstraction sheet

Choose one: a cinema booking app, a school canteen checkout, or a football score board.

Answer the six questions for it.

  1. What inputs do we need?
  2. What are the outputs and the outcome?
  3. What varies?
  4. What stays constant?
  5. What are the key actions?
  6. 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

Important

Abstraction keeps what matters and hides the rest. Name the inputs, outputs, what varies, what is constant, the key actions and what repeats.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.