25
Loops & List Intro2CS – week 3 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

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

25

Nested lists• We can place lists inside each other.