Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of...
-
Upload
emily-wilkinson -
Category
Documents
-
view
215 -
download
0
Transcript of Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of...
![Page 1: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/1.jpg)
1
Loops & List
Intro2CS – week 3
![Page 2: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/2.jpg)
2
Loops -- Motivation• Sometimes we want to repeat a certain set of
instructions more than once.
• The number of repetitions may be unknown in advance (depends on input).
• Two types of loops:– Repeat some instructions until some condition is met
(while loops)– Repeat some instructions for every item in a
sequence (for loops)
![Page 3: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/3.jpg)
3
The while loop
while <boolean expression>:<statements>
Evaluate boolean
expression
Execute statements
Skip statements (resume rest of
program)
TrueFalse
Start Here
![Page 4: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/4.jpg)
4
A simple example
![Page 5: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/5.jpg)
5
A simple example
![Page 6: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/6.jpg)
6
What if we forget?
The loop does not stop… This is a problem.
![Page 7: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/7.jpg)
7
How to check if a number is prime
![Page 8: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/8.jpg)
8
Break and Continue
Two statements to use in loops:
• break exits the loop immediately.
• continue skips remaining statements in loop
![Page 9: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/9.jpg)
9
Break, continue
![Page 10: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/10.jpg)
10
The for loop over sequences
A line that does not do anything.How else can you write this?
![Page 11: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/11.jpg)
11
Counting
Range objects represent a sequence of integers
range(stop)• represents ints from 0 to stop-1 (excluding stop)
range(start,stop)• represents ints from start to stop-1 (excluding stop)
range(start,stop,step)• represents ints from start to stop (exluding) with given
step size
![Page 12: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/12.jpg)
12
Counting• So what do these print?
![Page 13: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/13.jpg)
13
Checking if prime (again)
![Page 14: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/14.jpg)
14
Lists• We may want to save a large amount of data.
Example: the name of every person in class.
• Do we assign each to a different variable?
• What if we need to add a person?
• How do we process all this data in a loop?
![Page 15: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/15.jpg)
15
ListsUse [ ] to specify a list and assign it. Separate items with commas ,
Access items using their index:print(names[0])
print(names[-1])
print(len(names))
names[1] = “Robert”
print(names)
names = ["Alice", "Bob", "Charlie", "Dave", "Eve"]
Access to cell in list. Indices start at 0
Negative indices: count from the end.
Length of the list. Last index is length-1.
We can assign to cells (like regular variables)
![Page 16: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/16.jpg)
16
ListsAn empty list:
Other ways to create lists through the list “constructor”:
list() []
list(“word”) ['w', 'o', 'r', 'd']
list(range(5)) [0, 1, 2, 3, 4]
names = []
![Page 17: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/17.jpg)
17
Multiplication and addition
membership, deleting, appending
names = [“Alice”, “Bob”, “Charlie”, “Dave”]print("Bob" in names)
del names[1:3]print(names)
names.append(“Eve”)print(names)
print([1, 2, 3] * 3)
print([1, 2, 3] + [4, 5, 6, 7])
![Page 18: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/18.jpg)
18
Slicing
numbers = list(range(10))
numbers[:] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers[3:] [3, 4, 5, 6, 7, 8, 9]
numbers[:3] [0, 1, 2]
numbers[1:3] [1, 2]
numbers[::2] [0, 2, 4, 6, 8]
numbers[::-1] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Assign to slices:numbers[1:3] = ['a','b','c','d’]
[0, 'a', 'b', 'c', 'd', 3, 4, 5, 6, 7, 8, 9]
![Page 19: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/19.jpg)
19
loops over lists• We can loop over lists just like any other
sequence:
• Or:
(less elegant, but works)
for element in my_list:print(element)
for index in range(len(my_list)):print(my_list[index])
![Page 20: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/20.jpg)
20
Finding all primes
>>> print(all_primes_up_to(100))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
![Page 21: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/21.jpg)
The Sieve of Eratosthenes• Mark suspected prime numbers in a list
• The idea: once we find a prime number, we can erase all its multiples as primes.
• 2 is prime.
• Next “suspect” is also a prime (why?)
2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
20
21
T T T T T T T T T T T T T T T T T T T T
2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
20
21
T T F T F T F T F T F T F T F T F T F T
![Page 22: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/22.jpg)
The Sieve of Eratosthenes
In fact, we are done. Why?
Hint:
2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
20
21
T T F T F T F F F T F T F F F T F T F F
2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
20
21
T T F T F T F F F T F T F F F T F T F F
Not Prime, so multiples already erased!
![Page 23: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/23.jpg)
23
The Sieve of Eratosthenes
![Page 24: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/24.jpg)
24
![Page 25: Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697bfc61a28abf838ca718e/html5/thumbnails/25.jpg)
25
Nested lists• We can place lists inside each other.