0% found this document useful (0 votes)
58 views52 pages

Year 9 Lesson 2 Playlist

Uploaded by

aaroush199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views52 pages

Year 9 Lesson 2 Playlist

Uploaded by

aaroush199
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Year 9 Unit 2

Python programming with sequences of


data
ICT
Lesson 2
Playlist
Starter activity
Walkthrough
1
planets = ["Mercury", "Venus", Question .
2
3
"Earth", "Mars", What will be displayed on the screen
4 "Jupiter", "Saturn", when this program is executed?
5 "Uranus", "Neptune"]
6 A. Venus
position = 3 B. Earth
7
object = planets[position] C. Mars
print(object) D. Jupiter
Starter activity
Walkthrough: Answer

1
planets = ["Mercury", "Venus", planets When the program
2
"Earth", "Mars", 0 "Mercury" is executed, this is
3
4 "Jupiter", "Saturn", 1 "Venus" what the list of
5 "Uranus", "Neptune"] 2 "Earth" planets will look
6 3 "Mars" like in memory.
position = 3
7
object = planets[position] 4 "Jupiter"
5 "Saturn" Item numbering in
3
print(object) lists begins with 0.
6 "Uranus"
7 "Neptune"
Starter activity
Walkthrough: Answer
1
planets = ["Mercury", "Venus", Question .
2
3
"Earth", "Mars", What will be displayed on the screen,
4 "Jupiter", "Saturn", when this program is executed?
5 "Uranus", "Neptune"]
6 A. Venus
position = 3 B. Earth
7
object = planets[position] C. Mars
print(object) ▹ D. Jupiter
Outcomes
In this lesson, you will...
● Perform operations on lists
● Brush up on your Python skills (mainly selection)
Activity 1
The index of an item
1
planets = ["Mercury", "Venus", Question .
2
3
"Earth", "Mars", What do you think will be displayed
4 "Jupiter", "Saturn", on the screen when this program is
5 "Uranus", "Neptune"] executed?
6
position = planets.index("Venus")
print(position)
Activity 1
The index of an item
1
planets = ["Mercury", "Venus", planets When the
2
3
"Earth", "Mars", 0 "Mercury" program is
4 "Jupiter", "Saturn", 1 "Venus" executed, this is
5 "Uranus", "Neptune"] 2 "Earth" what the list of
6
position = planets.index("Venus") 3 "Mars" planets will look
print(position) 4 "Jupiter" like in memory.
5 "Saturn" Item numbering
6 "Uranus" in lists begins
7 "Neptune" with 0.
Activity 1
The index of an item
1
planets = ["Mercury", "Venus", Question .
2
3
"Earth", "Mars", What do you think will be displayed
4 "Jupiter", "Saturn", on the screen when this program is
5 "Uranus", "Neptune"] executed?
6
position = planets.index("Venus") Answer The number 1 will be
print(position) displayed.
This is the index of the value "Venus"
in the list of planets.
Activity 1
Operations on lists
There are many operations you can
perform on lists and their items. Some examples
list.append(item) add item at end of list numbers.append(42)
list.insert(index, item) add item at index cities.insert(2, "Oslo")
list.pop(index) remove item at index last = values.pop()
list.remove(item) remove item countries.remove("Japan")
list.index(item) search for index of item where = planets.index("Mars")
list.count(item) get occurrences of item nb_the = words.count("the")
list.reverse() reverse list values.reverse()
list.sort() sort list names.sort()
Activity 2

You will be using pair programming ,


with each member in a pair taking on a
specific role:

Driver
Control the keyboard and mouse
Navigator
Provide support and instructions

You will alternate between roles.


Activity 2
The solar system

Complete the tasks on your worksheet to


see how our view of the solar system has
evolved over time.
To perform the tasks, you will need to
perform operations on lists.
Activity 2
The solar system: Solutions
1
planets = ["Mercury", "Venus", Uranus (1781), Neptune (1846), and
2
3
"Earth", "Mars", Pluto (1930) were discovered later.
4
"Jupiter", "Saturn"]
Challenge .
5 print("Planets in antiquity:")
6 print(planets) Add these three planets to the list of
7 planets, one at a time.
8 Add Uranus to the list of planets
9 Add Neptune to the list of planets
10 Add Pluto to the list of planets
print("Planets by 1930:")
print(planets)
Activity 2
The solar system: Solutions
1 planets = ["Mercury", "Venus", Uranus (1781), Neptune (1846), and
2 "Earth", "Mars", Pluto (1930) were discovered later.
3 "Jupiter", "Saturn"]
Challenge .
4 print("Planets in antiquity:")
5 print(planets) Add these three planets to the list of
6 planets.append("Uranus") planets, one at a time.
7 planets.append("Neptune")
8 planets.append("Pluto")
9 print("Planets by 1930:")
10 print(planets)
Activity 2
The solar system: Walkthrough
1
planets = ["Mercury", "Venus", planets
2
3
"Earth", "Mars", 0 "Mercury"
4
"Jupiter", "Saturn"] 1 "Venus"
5 print("Planets in antiquity:") 2 "Earth"
6 print(planets) 3 "Mars"
7 4 "Jupiter"
8 planets.append("Uranus")
5 "Saturn"
9 planets.append("Neptune")
10 planets.append("Pluto")
print("Planets by 1930:")
print(planets)
Activity 2
The solar system: Walkthrough
1
planets = ["Mercury", "Venus", planets
2
3
"Earth", "Mars", 0 "Mercury"
4
"Jupiter", "Saturn"] 1 "Venus"
5 print("Planets in antiquity:") 2 "Earth"
6 print(planets) 3 "Mars"
7 4 "Jupiter"
8 planets.append("Uranus")
5 "Saturn"
9 planets.append("Neptune")
6 "Uranus"
10 planets.append("Pluto")
print("Planets by 1930:")
print(planets)
Activity 2
The solar system: Walkthrough
1
planets = ["Mercury", "Venus", planets
2
3
"Earth", "Mars", 0 "Mercury"
4
"Jupiter", "Saturn"] 1 "Venus"
5 print("Planets in antiquity:") 2 "Earth"
6 print(planets) 3 "Mars"
7 4 "Jupiter"
8 planets.append("Uranus")
5 "Saturn"
9 planets.append("Neptune")
6 "Uranus"
10 planets.append("Pluto")
7 "Neptune"
print("Planets by 1930:") 8 "Pluto"
print(planets)
Activity 2
The solar system: Solutions
9
print("Planets by 1930:") Poor Pluto was demoted in 2006: it was
10
print(planets) removed from the list of planets and
11
Remove Pluto from the list classified as a dwarf planet.
12
13 print("Planets after 2006:") Challenge .
print(planets)
Continue with your existing program.
Remove Pluto from the planets list.
Activity 2
The solar system: Solutions
9
print("Planets by 1930:") Poor Pluto was demoted in 2006: it was
10
print(planets) removed from the list of planets and
11
planets.remove("Pluto") classified as a dwarf planet.
12
13 print("Planets after 2006:") Challenge .
print(planets)
Continue with your existing program.
Remove Pluto from the planets list.
Activity 2
The solar system: Walkthrough
9
print("Planets by 1930:") planets
10
print(planets) 0 "Mercury"
11
planets.remove("Pluto")
1 "Venus"
12
2 "Earth"
13 print("Planets after 2006:")
3 "Mars"
print(planets)
4 "Jupiter"
5 "Saturn"
6 "Uranus"
7 "Neptune"
8 "Pluto"
Activity 2
The solar system: Walkthrough
9
print("Planets by 1930:") planets
10
print(planets) 0 "Mercury"
11
planets.remove("Pluto")
1 "Venus"
12
2 "Earth"
13 print("Planets after 2006:")
3 "Mars"
print(planets)
4 "Jupiter"
5 "Saturn"
6 "Uranus"
7 "Neptune"
Activity 2
The solar system: Solutions

1 dwarves = Create the list of dwarf planets In 2006, some sub-planetary objects
were classified as dwarf planets.
4
print("Dwarf planets:")
5 Challenge .
print(dwarves)
Create a list containing names of
dwarf planets: Ceres, Ataecina, Eris,
and Makemake.
Activity 2
The solar system: Solutions
1
dwarves = ["Ceres", "Ataecina", In 2006, some sub-planetary objects
"Eris", "Makemake"] were classified as dwarf planets.
4
5 print("Dwarf planets:")
Challenge .
print(dwarves)
Create a list containing names of
dwarf planets: Ceres, Ataecina, Eris,
and Makemake.
Activity 2
The solar system: Solutions
1
dwarves = ["Ceres", "Ataecina", In 2008, Haumea was recognised as
"Eris", "Makemake"] the official name of Ataecina.
2
Change second list item to Haumea
4 Challenge .
5 print("Dwarf planets:")
Continue with your existing program.
print(dwarves)
Modify the second item of the list.

Assign the value "Haumea" to it.


Activity 2
The solar system: Solutions
1
dwarves = ["Ceres", "Ataecina", In 2008, Haumea was recognised as
"Eris", "Makemake"] the official name of Ataecina.
2
dwarves[1] = "Haumea"
4 Challenge .
5 print("Dwarf planets:")
Continue with your existing program.
print(dwarves)
Modify the second item of the list.

Assign the value "Haumea" to it.


Activity 2
The solar system: Walkthrough
1
dwarves = ["Ceres", "Ataecina", dwarves
"Eris", "Makemake"] 0 "Ceres"
2 1 "Ataecina"
4
dwarves[1] = "Haumea" 2 "Eris"
5 print("Dwarf planets:") 3 "Makemake"
print(dwarves)
Activity 2
The solar system: Walkthrough
1
dwarves = ["Ceres", "Ataecina", dwarves
"Eris", "Makemake"] 0 "Ceres"
2 1 "Haumea"
4
dwarves[1] = "Haumea" 2 "Eris"
5 print("Dwarf planets:") 3 "Makemake"
print(dwarves)
Activity 2
The solar system: Solutions
1
dwarves = ["Ceres", "Ataecina", Pluto should also be added to the list.
"Eris", "Makemake"] Chronologically, it was the second to
2
dwarves[1] = "Haumea" be discovered.
3

4 Add Pluto as 2nd item Challenge .


5
print("Dwarf planets:") Continue with your existing program.
print(dwarves) Insert Pluto as the second item in the
list.
Activity 2
The solar system: Solutions
1
dwarves = ["Ceres", "Ataecina", Pluto should also be added to the list.
"Eris", "Makemake"] Chronologically, it was the second to
2
dwarves[1] = "Haumea" be discovered.
3

4 dwarves.insert(1, "Pluto") Challenge .


5
print("Dwarf planets:") Continue with your existing program.
print(dwarves) Insert Pluto as the second item in the
list.
Activity 2
The solar system: Walkthrough
1
dwarves = ["Ceres", "Ataecina", dwarves
"Eris", "Makemake"] 0 "Ceres"
2 1 "Haumea"
3
dwarves[1] = "Haumea" 2 "Eris"
4 dwarves.insert(1, "Pluto") 3 "Makemake"
5
print("Dwarf planets:")
print(dwarves)
Activity 2
The solar system: Walkthrough
1
dwarves = ["Ceres", "Ataecina", dwarves
"Eris", "Makemake"] 0 "Ceres"
2 1 "Pluto"
3
dwarves[1] = "Haumea" 2 "Haumea"
4 dwarves.insert(1, "Pluto") 3 "Eris"
5
print("Dwarf planets:") 4 "Makemake"
print(dwarves)
Activity 3
Dice battle game
Roll The attacker rolls three dice. The defender rolls two.

Sort Each player’s rolls are sorted in descending order (highest first).

Check Compare the players’ highest roll: lower loses a point.


Compare the players’ second highest roll: lower loses a point.
Ties are won by the defender.
Activity 3
Dice battle game: Example 1
Roll The attacker rolls three dice. The Example: Roll
defender rolls two.

Sort Each player’s rolls are sorted in


descending order (highest first).

Check Compare the players’ highest roll:


lower loses a point.
Compare the players’ second highest roll:
lower loses a point.
Ties are won by the defender.
Activity 3
Dice battle game: Example 1
Roll The attacker rolls three dice. The Example: Sort
defender rolls two.

Sort Each player’s rolls are sorted in


descending order (highest first).

Check Compare the players’ highest roll:


lower loses a point.
Compare the players’ second highest roll:
lower loses a point.
Ties are won by the defender.
Activity 3
Dice battle game: Example 1
Roll The attacker rolls three dice. The Example: Check
defender rolls two.

Sort Each player’s rolls are sorted in


descending order (highest first).

Check Compare the players’ highest roll:


lower loses a point. -1 -1

Compare the players’ second highest roll:


lower loses a point.
Ties are won by the defender.
Activity 3
Dice battle game: Example 2
Roll The attacker rolls three dice. The Example: Roll
defender rolls two.

Sort Each player’s rolls are sorted in


descending order (highest first).

Check Compare the players’ highest roll:


lower loses a point.
Compare the players’ second highest roll:
lower loses a point.
Ties are won by the defender.
Activity 3
Dice battle game: Example 2
Roll The attacker rolls three dice. The Example: Sort
defender rolls two.

Sort Each player’s rolls are sorted in


descending order (highest first).

Check Compare the players’ highest roll:


lower loses a point.
Compare the players’ second highest roll:
lower loses a point.
Ties are won by the defender.
Activity 3
Dice battle game: Example 2
Roll The attacker rolls three dice. The Example: Check
defender rolls two.

Sort Each player’s rolls are sorted in


descending order (highest first).

Check Compare the players’ highest roll:


lower loses a point. -1 -1

Compare the players’ second highest roll:


lower loses a point.
Ties are won by the defender.
Activity 3
Dice battle game
Create a program for this game by
completing the task on your worksheet.
Activity 3
Dice battle game: Solutions
4
attacker = dicerolls(3)
5
defender = dicerolls(2)
6
7 print("Players’ rolls")
8 print("Attacker:", attacker)
9 print("Defender:", defender)
10
Sort the Attacker’s dice
11
12 Sort the Defender’s dice
13
print("Sorted")
print("Attacker:", attacker)
print("Defender:", defender)
Activity 3
Dice battle game: Solutions
4
attacker = dicerolls(3)
5
defender = dicerolls(2)
6
7 print("Players’ rolls")
8 print("Attacker:", attacker)
9 print("Defender:", defender)
10
attacker.sort(reverse=True) You can also sort in ascending order and
11
12 defender.sort(reverse=True) then reverse.
13
print("Sorted")
print("Attacker:", attacker)
print("Defender:", defender)
Activity 3
Dice battle game: Solutions
9
attacker.sort(reverse=True)
10
defender.sort(reverse=True)
11
12 print("Sorted")
13 print("Attacker:", attacker)
14 print("Defender:", defender)
15
16 if attacker[0] > defender[0]: Compare the two players’ highest
17 defender_points = defender_points - 1 rolls. The rolls have been sorted,
else: so you know that the highest rolls
attacker_points = attacker_points - 1 are first.
Activity 3
Dice battle game: Solutions
9 attacker.sort(reverse=True)
10 defender.sort(reverse=True)
11
print("Sorted")
12
print("Attacker:", attacker)
13
print("Defender:", defender)
14
if attacker[0] > defender[0]:
15
defender_points = defender_points - 1
16
17
else:
attacker_points = attacker_points - 1
18
19 if attacker[1] > defender[1]: Compare the two players’ second
defender_points = defender_points - 1
20 highest rolls.
21 else:
attacker_points = attacker_points - 1
Activity 4
Measuring length
The built-in len function returns the length Examples
(number of items) of a list.
nb_dwarves = len(dwarves)
Syntax len(list) if seats > len(guests):
print("Insufficient seats")

while len(rolls) < 100:


dice = randint(1,6)
rolls.append(dice)
Activity 4
Measuring length
1
planets = ["Mercury", "Venus", Question .
2
3
"Earth", "Mars", What will be displayed on the screen,
4 "Jupiter", "Saturn", when this program is executed?
5 "Uranus", "Neptune"]
6
length = len(planets) Answer 8, which is the number of item
print(length) in the list of planets.
Activity 4
List membership
The in operator checks if a value is equal Examples
to any item in a list.
if "chips" in supplies:
Expressions formed with in evaluate to supplies.remove("chips")
either True or False.
if not word in sensored:
Syntax item in list print("You can use", word)

invited = myname in guests


Activity 4
List membership
1
planets = ["Mercury", "Venus", Question .
2
3
"Earth", "Mars", What will be displayed on the screen,
4 "Jupiter", "Saturn", when this program is executed?
5 "Uranus", "Neptune"]
6
status = "Pluto" in planets
print(status) Answer False, which is the value of
the expression "Pluto" in planets.
There is no item equal to "Pluto" in
the list of planets.
Activity 4
Literature
1
from ncce.textfile import words This code retrieves the entire text of
2
wordlist = words('dracula.txt') Bram Stoker’s Dracula and stores it as
a list of words (wordlist).

Note The textfile module and the


words function are not standard
Python components.
Activity 4
Literature
1
from ncce.textfile import words Question .
2
wordlist = words('dracula.txt')
3 What does line 3 compute?
length = len(wordlist)
Answer How many words there are
in the book.
Activity 4
Literature
1
from ncce.textfile import words Question .
2
wordlist = words('dracula.txt')
3 What does line 5 compute?
4 length = len(wordlist)
5 print(length, "words in Dracula") Answer Whether or not "vampire"
appears in the book (True or False)
contains = "vampire" in wordlist
Question .

How can you modify the program to


compute the number of times that
"vampire" appears in the book?
Activity 4
Literature
1
from ncce.textfile import words How many times do you think that the
2
wordlist = words('dracula.txt') word "vampire" appears in the book?
3
4 length = len(wordlist)
5 print(length, "words in Dracula")
6
Run the program to see for yourselves
times = wordlist.count('vampire') (ncce.io/py-books-1)
print(times)
Recap
Name some of the operations that can be
performed on lists. Reversing

Adding items Sorting


Searching
(appending, inserting)
membership,
finding the index of an item,
Deleting items counting occurrences
(popping, removing)
Summary
In this lesson, you... Next lesson, you will...
Performed operations on lists Use iteration (while statements) to
Brushed up on your Python skills (mainly control the flow of program execution
selection)
Practise using common operations on
lists

Perform operations on strings

You might also like