SLR 2.
1 Algorithms | Abstraction OCR GCSE (J277)
Decomposi
tion
Abstractio
n
Algorithmi
c thinking
Starter: What is Abstraction?
SLR 2.1 Algorithms | Abstraction OCR GCSE (J277)
Abstraction
Abstraction is the process of removing
unnecessary details and including only
the relevant details.
It is a method of computational thinking
that focuses on what is important when
solving problems.
Learning objectives (L1-4)
● Understand what the three basic programming constructs are and how they
are used to control the flow of a program.
● Identify and correct common errors in computer programs.
● Be able to use inputs, outputs, arithmetic and string handling.
● Apply knowledge of computational thinking to solve complex problems.
Matching task
1. input A. perform actions repeatedly
2. print B. display to the screen
3. while C. check a condition, to select which
4. if actions to perform
5. = D. read from the keyboard
E. assign a value to a variable
Matching task
1. input A. perform actions repeatedly
2. print B. display to the screen
3. while C. check a condition, to select which
4. if actions to perform
5. = D. read from the keyboard
E. assign a value to a variable
Worksheet 1
Walkthrough
1 a = int(input()) When this program is executed, the user types in 12
2 b = int(input()) and 2 on the keyboard.
3 avg = b + a / 2 Question .
4 print(avg)
What will be displayed on the screen?
A. 2 + 12 / 2
B. 13
C. 7
▹ D. 8
Remember the rules of operator precedence: division
before addition.
Worksheet 1
Walkthrough
1 a = int(input()) When this program is executed, the user types in 12
2 b = int(input()) and 2 on the keyboard.
3 max = a Question .
4 if b > max:
5
What will be displayed on the screen?
max = b
6 print(max) ▹ A. Nothing
B. 2
C. 12
D. max
The condition in line 4 will evaluate to False. The
print in line 6 is indented: it is part of the if–block, so
it will not be executed.
Worksheet 1
Identify
1 a = int(input()) variables
2 b = int(input())
3 avg = b + a / 2 assignment statements
4 print(avg)
5 max = a arithmetic expression
6 if b > max:
7 max = b Boolean expression (condition)
8 print(max)
a block of statements that may not be
executed
Worksheet 1
Identify: Answers
1 a = int(input()) variables a, b, avg, and max
2 b = int(input())
3 avg = b + a / 2 assignment statements
4 print(avg)
5 max = a arithmetic expression
6 if b > max:
7 max = b Boolean expression (condition)
8 print(max)
a block of statements that may not be
executed
Recap 1
Selection recap
if condition
: You need a selection structure . (if-elif-
block of else) when there are multiple branches
statements
condition
and your program needs to select which
elif :
block of
one of them to follow.
statements
else:
You can use none, one, or multiple elif–blocks.
block of
statements You can use none, or one else–block.
Recap 1
Selection recap
1 print("What day is it today?") Let’s extend this program together, to
2 day = int(input())
check the day and display whether it’s a
weekday on not.
Note: The program uses an integer for
each day of the week, ranging from 0 for
Monday to 6 for Sunday.
Live coding (ncce.io/py-week-0)
Recap 1
Selection recap: After the live coding session
1 print("What day is it today?") We created a program to check the day
2 day = int(input())
and display whether it’s a weekday on not.
3 if day <= 4:
4 print("It’s a weekday") Note: The program uses an integer for
5 else: each day of the week, ranging from 0 for
6 print("It’s the weekend!")
Monday to 6 for Sunday.
Worksheet 2
How long until the weekend?
Complete Worksheet 1 and 2. You will
need to carefully read through the
worksheet.
Don’t forget to use your online compiler
when you think it may help you!
Worksheet 2
How long until the weekend? Solution
1 print("What day is it today?")
2 day = int(input())
3 if day < 4:
4 print("It’s a weekday")
5 remaining = 5 - day Remaining days .
6 print(remaining, "days until the weekend") (steps 2-3).
7 else:
8 print("It’s the weekend!")
Worksheet 2
How long until the weekend? Solution
1 print("What day is it today?")
2 day = int(input())
3 if day < 4:
4 print("It’s a weekday")
5 remaining = 5 - day
6 print(remaining, "days until the weekend")
7 elif day == 4: Handle Friday as a special case .
8 print("It’s Friday") (step 4).
9 print("Just a day left until the weekend")
10 else:
11 print("It’s the weekend!")
Worksheet 2
How long until the weekend? Solution
1 from datetime import datetime Retrieve the current day of the week .
2 day = datetime.now().weekday() (explorer task)
3 if day < 4:
4 print("It’s a weekday")
5 remaining = 5 - day
6 print(remaining, "days until the weekend")
7 elif day == 4:
8 print("It’s Friday")
9 print("Just a day left until the weekend")
10 else:
11 print("It’s the weekend!")
Recap 2
A list of names
1 days = ["Monday", "Tuesday", The names for the days of the week can be
2 "Wednesday", "Thursday",
3
stored in a list .
"Friday", "Saturday",
4 "Sunday"]
Syntax: A comma-separated list of
values (items), in square brackets.
In this example, the list items are string
literals (i.e. pieces of text), so they need
to be in quotation marks.
Recap 2
A list of names
1 days = ["Monday", "Tuesday", A list is a kind of data structure .
2 "Wednesday", "Thursday",
3 "Friday", "Saturday", Data structures are organised collections
4 "Sunday"] of data.
In the case of lists, data is organised in a
sequence, with each item having a unique
index, denoting its position in the list.
Recap 2
A list of names
1 days = ["Monday", "Tuesday", days 0 "Monday"
2 "Wednesday", "Thursday", 1 "Tuesday"
3 "Friday", "Saturday",
4 2 "Wednesday"
"Sunday"]
3 "Thursday"
4 "Friday"
5 "Saturday"
6 "Sunday"
When the program is executed, this is
what the list will look like in memory.
Recap 2
A list of names
1 days = ["Monday", "Tuesday", days 0 "Monday" Note: List item
2 "Wednesday", "Thursday", numbering starts
1 "Tuesday"
3 "Friday", "Saturday", from 0 (zero-
4 2 "Wednesday"
"Sunday"] based).
3 "Thursday"
4 "Friday"
5 "Saturday"
6 "Sunday"
Each item has a unique index, denoting its
position in the list.
Recap 2
A number for a name
1 days = ["Monday", "Tuesday", days 0 "Monday"
2 "Wednesday", "Thursday", 1 "Tuesday"
3 "Friday", "Saturday",
4 2 "Wednesday"
"Sunday"]
5 3 "Thursday"
print(days[0])
4 "Friday"
5 "Saturday"
6 "Sunday"
When this program is executed, what will
be displayed on the screen?
Answer: Monday
Recap 2
A number for a name
1 days = ["Monday", "Tuesday", days 0 "Monday"
2 "Wednesday", "Thursday", 1 "Tuesday"
3 "Friday", "Saturday",
4 2 "Wednesday"
"Sunday"]
5 3 "Thursday"
print(days[1])
4 "Friday"
5 "Saturday"
6 "Sunday"
When this program is executed, what will
be displayed on the screen?
Answer: Tuesday
Recap 2
A number for a name
1 days = ["Monday", "Tuesday", days 0 "Monday"
2 "Wednesday", "Thursday", 1 "Tuesday"
3 "Friday", "Saturday",
4 2 "Wednesday"
"Sunday"]
5 3 "Thursday"
print(days[4])
4 "Friday"
5 "Saturday"
6 "Sunday"
When this program is executed, what will
be displayed on the screen?
Answer: Friday
Recap 2
A number for a name
1 days = ["Monday", "Tuesday", days 0 "Monday"
2 "Wednesday", "Thursday", 1 "Tuesday"
3 "Friday", "Saturday",
4 2 "Wednesday"
"Sunday"]
5 3 "Thursday"
print(days[7])
4 "Friday"
5 "Saturday"
6 "Sunday"
When this program is executed, what will
be displayed on the screen?
Answer: An IndexError will occur.
Recap 2
A number for a name
1 days = ["Monday", "Tuesday", days 0 "Monday" day 3
2 "Wednesday", "Thursday", 1 "Tuesday"
3 "Friday", "Saturday",
4 2 "Wednesday"
"Sunday"]
5 3 "Thursday"
day = 3
6 print(days[day]) 4 "Friday"
5 "Saturday"
6 "Sunday"
Expressions can be used to access item
lists (as long as they evaluate to an
integer).
Recap 2
A number for a name
1 days = ["Monday", "Tuesday", days 0 "Monday" day 3
2 "Wednesday", "Thursday", 1 "Tuesday"
3 "Friday", "Saturday",
4 2 "Wednesday"
"Sunday"]
5 3 "Thursday"
day = 3
6 print(days[day]) 4 "Friday"
3 5 "Saturday"
6 "Sunday"
When this program is executed, what will
be displayed on the screen?
Answer: Thursday
Recap 2
A number for a name
1 days = ["Monday", "Tuesday", days 0 "Monday" day 3
2 "Wednesday", "Thursday", 1 "Tuesday"
3 "Friday", "Saturday",
4 2 "Wednesday"
"Sunday"]
5 3 "Thursday"
day = 3
6 print(days[day-1]) 4 "Friday"
2 5 "Saturday"
6 "Sunday"
When this program is executed, what will
be displayed on the screen?
Answer: Wednesday
Recap 2
A number for a name
1 days = ["Monday", "Tuesday", Examine an extended version
2 "Wednesday", "Thursday",
3
(ncce.io/py-week-5) of the previous
"Friday", "Saturday",
4 "Sunday"] program, which incorporates the list of
days.
Worksheet 3
As seasons roll on by
Complete Worksheet 3. You will need to
carefully read through the worksheet.
Don’t forget to use your online compiler
when you think it may help you!
Worksheet 3
As seasons roll on by: Solutions
1 months = ["January", "February",
2 "March", "April", "May",
3 "June", "July", "August",
4 "September", "October", "November",
5 "December"]
6 print("These are the summer months:")
7 print(months[5]) June has an index of 5.
8 print(months[6]) July has an index of 6.
9 print(months[7]) August has an index of 7.
All indices are shifted by 1, compared to the
common numbering of months, because list
item numbering is zero-based.
Worksheet 3
As seasons roll on by: Solutions
1 months = ["January", "February",
2 "March", "April", "May",
3 "June", "July", "August",
4 "September", "October", "November",
5 "December"]
6 print("What month is it? (1-12)")
7 month = int(input())
8 print("It is", months[month-1]) The month (provided by the user) is reduced by
1, before it is used as an index, because list
item numbering is zero-based.
Worksheet 3
As seasons roll on by: Solutions
1 seasons = ["Winter", "Spring", "Summer", "Autumn"]
2 print("What month is it? (1-12)")
3 month = int(input())
4 if month <= 2 or month == 12: The condition that checks if this is a winter
5 season = 0 month is more complex, because the
6 elif month <= 5: numbering for winter months is not
7 season = 1 consecutive.
8 elif month <= 8:
9 season = 2
10 else:
11 season = 3
12 print("It is", seasons[season])
Homework
• Start revising computational thinking, you should have already covered this topic and
I will be using starters as a recap.
• L4 will include an assessment to see what you remember.