Python Programming With Sequences of Data - Y9

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

Lesson 4:

The famous for


Year 9 – Python programming with sequences of data
Starter activity

Shopping list

1 shopping = ["Pasta", "Tomatoes", This is a shopping list.


2 "Onions", "Basil",
3 "Parmesan"] Question .
4 print("Buy:") What do you think will be displayed on the screen
5 for item in shopping: when this program is executed?
6 print(item)
Buy:
Pasta
Tomatoes
Onions
Basil
Parmesan
Starter activity

for loops: Syntax and use

for variable
in : list for loops are convenient for iterating over
block of
statements
the items of a list

Note for loops can be used for iterating over any


sequence of elements, e.g. strings.
Objectives

In this lesson, you will...

● Use iteration (for statements) to


iterate over list items
● Practise using common operations on
lists and strings
Activity 1

Iterating over a list of dice rolls

1 rolls = [1, 4, 3, 6] This is a list of dice rolls.


2 for dice in rolls:
3 Question .
print(dice)
What do you think will be displayed on the screen
when this program is executed?

1
4
3
6
Activity 1

Iterating over a list of dice rolls

1 rolls = [1, 4, 3, 6] This is a list of dice rolls.


2 count = 0
3 Question .
for dice in rolls:
4 count = count + 1 What do you think will be displayed on the screen
5
when this program is executed?
print(count)
4
The count variable is initialised to 0 and is
incremented by 1 for each item in the list.
Activity 1

Iterating over a list of dice rolls

1 rolls = [1, 4, 3, 6] This is a list of dice rolls.


2 count = 0
3 Question .
for dice in rolls:
4 if dice > 3: What do you think will be displayed on the screen
5 count = count + 1 when this program is executed?
6 print(count) 2
The count variable is initialised to 0 and is
incremented by 1 for each item in the list that is
greater than 3.
Activity 1

Iterating over a list of dice rolls

1 rolls = [1, 4, 3, 6] This is a list of dice rolls.


2 selection = []
3 Question .
for dice in rolls:
4 if dice > 3: What do you think will be displayed on the screen
5 selection.append(dice) when this program is executed?
6 print(selection) [4, 6]
selection is initialised to an empty list [].
The value of each dice roll in rolls that is greater than
3 is appended to selection.
Activity 1

Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls
3 for dice in rolls:
4 if dice > 3:
0 1
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
Activity 1

Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3: False
0 1 1
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
Activity 1

Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3: True
0 1 4
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
Activity 1

Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3:
0 1 4
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
0 4
Activity 1

Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3: False
0 1 3
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
0 4
Activity 1

Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3: True
0 1 6
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
0 4
Activity 1

Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3:
0 1 6
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
0 4
1 6
Activity 1

Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls
3 for dice in rolls:
4 if dice > 3:
0 1
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
0 4
1 6
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

Lipogram

The 1939 novel Gadsby, by Ernest Vincent


Wright is a lipogram (missing a letter).
Activity 2

Lipogram

A note at the end of the book claims: “Not


a word containing the letter ‘E’ has
appeared in this story of over 50,000
words.”
We will make a program to check this
claim.
Activity 2

Lipogram

1 from ncce.textfile import words This code retrieves the entire text of
2 wordlist = words('gadsby.txt')
Gadsby and stores it as a list of words
(wordlist).
Note The textfile module and the words function are
not standard Python components.

We will make a program that checks the


claim that no words containing ‘E’ are to
be found in the book.
Live coding (ncce.io/py-books-2)
Activity 2

Lipogram: After the live coding session

1 from ncce.textfile import words


2 wordlist = words('gadsby.txt')
3 length = len(wordlist) Determine the number of words in the book
4 print(length, "words in Gadsby") (the length of the wordlist).
5 for word in wordlist: Iterate over the wordlist and print any word
6 if "e" in word: that contains "e".
7 print(word)
Activity 3

English words

Use for to iterate over a list containing


thousands of English words, to complete
the tasks from your worksheet.
Activity 3

English words: Task 1 solution

1 from ncce.data import dictionary


2 nb_words = len(dictionary)
3 print(nb_words, "english words in the list")
4 print("Length of words to search for:")
5 length = int(input())
6 count = 0 Count words of a given length:
7 for word in dictionary: iterate over the en_words and increase the
8 if len(word) == length: count when the length of the current word is
9 count = count + 1 equal to the given length.
10 print("There are", count, "words with", length, "letters")
Activity 3

English words: Task 2 solution

1 from ncce.data import dictionary


2 print("Text to search for:")
3 sub = input()
4 collection = [] Collect words that contain a given string:
5 for word in dictionary: iterate over the en_words and append to the
6 if sub in word: collection when the current word contains the
7 collection.append(word) given sub-string.
8 for word in collection: Print every word in the collection.
9 print(word)
Activity 3

English words: Explorer task solution

1 from ncce.data import dictionary The successive values of longest, as the


2 longest = "" program iterates over the words.
3 a
for word in dictionary: aa
4 if len(word) > len(longest): aaa
aahed
5 longest = word aahing
aardvark
6 aardvarks
print("The longest word is", longest) aardwolves
abacination
abalienating
abarticulation
abdominoanterior
abdominohysterectomy
acetylphenylhydrazine
alkylbenzenesulfonates
anthropomorphologically
antidisestablishmentarian
antidisestablishmentarianism
cyclotrimethylenetrinitramine
dichlorodiphenyltrichloroethane (31 letters)
Activity 4

Heartbeats

So far, you have used for to iterate over


lists of textual data.
In this activity, you will iterate over lists
of numbers.
Activity 4

Heartbeats

Use your worksheet to process real ECG


(electrocardiogram) data from a medical
database, to detect heartbeats.
Activity 4

Heartbeats: Solutions

1 from ncce.mitdb_data import load, plot


2 heartbeat_data = load(1000)
3 plot(heartbeat_data, 'heartbeats.png')
4 previous = -1.0
5 for value in heartbeat_data:
Print "heartbeat detected"
every time value is positive and
its previous value is negative.

previous = value Assign the current value to previous.


In the next iteration, value will be updated,
but previous will hold its value from the
current iteration.
Activity 4

Heartbeats: Solutions

1 from ncce.mitdb_data import load, plot


2 heartbeat_data = load(1000)
3 plot(heartbeat_data, 'heartbeats.png')
4 previous = -1.0
5 for value in heartbeat_data:
6 if value > 0:
7 if previous < 0:
8 print("heartbeat detected")
9 previous = value Assign the current value to previous.
In the next iteration, value will be updated,
but previous will hold its value from the
current iteration.
Activity 4

Heartbeats: Solutions

1 from ncce.mitdb_data import load, plot


2 heartbeat_data = load(1000)
3 plot(heartbeat_data, 'heartbeats.png')
4 previous = -1.0
5 for value in heartbeat_data:
6 if value > 0 and previous < 0:
7 print("heartbeat detected")

8 previous = value Assign the current value to previous.


In the next iteration, value will be updated,
but previous will hold its value from the
current iteration.
Activity 4

Heartbeats: Solutions

1 from ncce.mitdb_data import load, plot


2 mins = 15
3 heartbeat_data = load(mins*60*360) 15-minutes worth of data,
4 count = 0 at 360 values per second
5 previous = -1.0
6 for value in heartbeat_data:
7 if value > 0 and previous < 0:
8 count = count + 1
9 previous = value
10 print(count, "heartbeats detected")
11 print("average heart rate is", count/mins)
Plenary

What’s this for?

1 word = "lipogram" Question .


2 for item in word: What do you think will be displayed on the screen
3 print(item) when this program is executed?

l Note . You can use for to iterate over


i anything that has individual elements.
p In this case, for iterates over the characters
o of a string.
g
r
a
m
Plenary

What’s this for?

1 syllables = ["li", "po", "gram"] Explorer task .


2 for syllable in syllables: What do you think will be displayed on the screen
3 for letter in syllable: when this program is executed?
4 print(letter)
l
i
p
o
g
r
a
m
Summary

In this lesson, you... In the next lesson, you will...

Used iteration (for statements) to iterate Use variables to keep track of counts and
over list items sums

Practised using common operations on Use iteration (for statements) to iterate


lists and strings over strings of characters

Develop guided mini–projects that will


help you apply what you have learnt

You might also like