For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to...

24
For-Loops Presentation 13

Transcript of For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to...

Page 1: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

For-Loops

Presentation 13

Page 2: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Announcements for This Lecture

Lessons

• Videos 16.1-16.7 for today• Videos 17.1-17.5 next time

Assignments/Lab

• A3 is due Tomorrow§ Survey is now posted§ Will be graded before exam

• A4 posted on Monday§ Longer time to do this one§ Covers this lecture and next

• Work on Today’s lab!§ Lists will be on the exam§ But not for-loops (today)

10/15/20 For Loops 2

• Prelim, 10/18 at 7:30 pm§ Material up to TUESDAY§ Study guide is posted

• Review TONIGHT§ Zoom link on website§ Will be recorded

Page 3: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Activity Time: For-Loops

Function Definitions

def reverse(q):r = []for x in q:

r = [x]+rreturn r

Function Call

>>> p = [5,7]>>> r = reverse(p)

26252423

10/15/20 For Loops 3

What does memory look like at start?

Page 4: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

B:

C: D:

A:

Which One is Closest to Your Answer?

reverse 23

id1q

10/15/20 For Loops 4

r

id1p

id1list

57

01

reverse 23

id1q

id1p

id1list

57

01

reverse 23

pq

id1p

id1list

57

01

reverse 23

[5,7]q

[5,7]p

Page 5: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

B:

C: D:

A:

Which One is Closest to Your Answer?

reverse 23

id1q

10/15/20 For Loops 5

r

id1p

id1list

57

01

reverse 23

id1q

id1p

id1list

57

01

reverse 23

pq

id1p

id1list

57

01

reverse 23

[5,7]q

[5,7]p

E:

¯\_(ツ)_/¯

Page 6: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Activity Time: For-Loops

Function Definitions

def reverse(q):r = []for x in q:

r = [x]+rreturn r

Function Call

>>> p = [5,7]>>> r = reverse(p)

26252423

10/15/20 For Loops 6

B:reverse 23

id1q

id1p

id1list

57

01What is the next step?

Page 7: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

B:

C: D:

A:

Which One is Closest to Your Answer?

reverse 24

id1q

10/15/20 For Loops 7

[]r

id1p

id1list

57

01

reverse 24

id1q

r

id1p

id1list

57

01

reverse 24

id1q

id2r

id1p

id1list

57

01

id2list

reverse 25

id1q

id2r

id1p

id1list

57

01

id2list

70

Page 8: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Activity Time: For-Loops

Function Definitions

def reverse(q):r = []for x in q:

r = [x]+rreturn

Function Call

>>> p = [5,7]>>> r = reverse(p)

26252423

10/15/20 For Loops 8

What is the next step?

C:reverse 24

id1q

id2r

id1p

id1list

57

01

id2list

Page 9: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

B:

C: D:

A:

Which One is Closest to Your Answer?

reverse 24

id1q

10/15/20 For Loops 9

id2r

id1p

id1list

57

01

id2list

50

id1p

id1list

57

01

id2list

id1p

id1list

57

01

id1list

id1p

id1list

57

01

id1list

x 5

reverse 24

id1q

id2r

x 5

reverse 25

id1q

id2r

x [5]

reverse 25

id1q

id2r

x 5

Page 10: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Activity Time: For-Loops

Function Definitions

def reverse(q):r = []for x in q:

r = [x]+rreturn

Function Call

>>> p = [5,7]>>> r = reverse(p)

26252423

10/15/20 For Loops 10

What is the next step?

B: id1p

id1list

57

01

id2list

reverse 25

id1q

id2r

x 5

Page 11: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

B:

C: D:

A:

Which One is Closest to Your Answer?

reverse 24

id1q

10/15/20 For Loops 11

id2r

id1p

id1list

57

01

id2list

50

id1p

id1list

57

01

id3list

50

id1p

id1list

57

01

id2list

50

id2p

id1list

57

01

id3list

50

x 5

reverse 24

id1q

id2r

x 7

reverse 24

id1q

id3r

x 7

reverse 24

id1q

id3r

x 5

Page 12: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Activity Time: For-Loops

Function Definitions

def reverse(q):r = []for x in q:

r = [x]+rreturn

Function Call

>>> p = [5,7]>>> r = reverse(p)

26252423

10/15/20 For Loops 12

What is the next step?

B: id1p

id1list

57

01

id3list

50

reverse 24

id1q

id3r

x 5

Page 13: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Let’s Skip Ahead One

Function Definitions

def reverse(q):r = []for x in q:

r = [x]+rreturn

Function Call

>>> p = [5,7]>>> r = reverse(p)

26252423

10/15/20 For Loops 13

What is the next step?

id1p

id1list

57

01

id3list

50

reverse 25

id1q

id3r

x 7

Page 14: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

B:

C: D:

A:

Which One is Closest to Your Answer?

reverse 24

id1q

10/15/20 For Loops 14

id4r

id1p

id1list

57

01

id4list

57

01

id1p

id1list

57

01

id4list

75

01

id1p

id1list

57

01

id4list

57

01

id1p

id1list

57

01

id4list

75

01

x 7

reverse 26

id1q

id4r

x 5

reverse 26

id1q

id4r

x 5

reverse 24

id1q

id4r

x 5

Page 15: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Let’s Skip Ahead A Bit

Function Definitions

def reverse(q):r = []for x in q:

r = [x]+rreturn

Function Call

>>> p = [5,7]>>> r = reverse(p)

26252423

10/15/20 For Loops 15

Still return when empty

B: id1p

id1list

57

01

id4list

75

01

reverse 24

id1q

id4r

x 5

Page 16: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Simple Accumulator

def avg(tup):"""Returns average of all of the elements in the tuple.

Remember that the average of a tuple is the sum of all of the elements in the tuple divided by the number of elements.

Parameter tup: the tuple to checkPrecondition: tup is a tuple of numbers (int or float)"""pass

10/15/20 For Loops 16

Page 17: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Simple Accumulator

def avg(tup):"""Returns average of all of the elements in the tuple.

Remember that the average of a tuple is the sum of all of the elements in the tuple divided by the number of elements.

Parameter tup: the tuple to checkPrecondition: tup is a tuple of numbers (int or float)"""pass

10/15/20 For Loops 17

Loop over?A: ElementsB: PositionsC: Doesn’t matter

Page 18: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Another Accumulatordef skip(s,n):

"""Returns a copy of s, only including positions that are multiples of n

A position is a multiple of n if pos % n == 0.

Examples: skip('hello world',1) returns 'hello world'skip('hello world',2) returns 'hlowrd'skip('hello world',3) returns 'hlwl'

Parameter s: the string to copyPrecondition: n is an int > 0"""pass

10/15/20 For Loops 18

Page 19: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Another Accumulatordef skip(s,n):

"""Returns a copy of s, only including positions that are multiples of n

A position is a multiple of n if pos % n == 0.

Examples: skip('hello world',1) returns 'hello world'skip('hello world',2) returns 'hlowrd'skip('hello world',3) returns 'hlwl'

Parameter s: the string to copyPrecondition: n is an int > 0"""pass

10/15/20 For Loops 19

Loop over?A: ElementsB: PositionsC: Doesn’t matter

Page 20: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

No (???) Accumulator

def first_vowel(w):"""Returns the position of the first vowel; -1 if there are no vowels.

The vowels are 'a', 'e', 'i', 'o', 'u'. The letter 'y' counts as vowel only if it is not the first element of w.

Parameter w: the word to searchPrecondition: w is a nonempty string with only lowercase letters"""pass

10/15/20 For Loops 20

Page 21: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

No (???) Accumulator

def first_vowel(w):"""Returns the position of the first vowel; -1 if there are no vowels.

The vowels are 'a', 'e', 'i', 'o', 'u'. The letter 'y' counts as vowel only if it is not the first element of w.

Parameter w: the word to searchPrecondition: w is a nonempty string with only lowercase letters"""pass

10/15/20 For Loops 21

Loop over?A: ElementsB: PositionsC: Doesn’t matter

Page 22: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Mutable For-Loop

def reverse(alist):"""Reverses the list in place

Examples:If a = [1,2,2,3,4], reverse(a) changes a to [4,3,2,2,1]If a = [1,2,1], reverse(a) changes a to [1,2,1]If a = [], reverse(a) changes a to []

Parameter alist: the list to modifyPrecondition: alist is a list of numbers (float or int)"""pass

10/15/20 For Loops 22

Page 23: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

Mutable For-Loop

def reverse(alist):"""Reverses the list in place

Examples:If a = [1,2,2,3,4], reverse(a) changes a to [4,3,2,2,1]If a = [1,2,1], reverse(a) changes a to [1,2,1]If a = [], reverse(a) changes a to []

Parameter alist: the list to modifyPrecondition: alist is a list of numbers (float or int)"""pass

10/15/20 For Loops 23

Loop over?A: ElementsB: PositionsC: Doesn’t matter

Page 24: For-Loops · Presentation 13. Announcements for This Lecture Lessons ... Which One is Closest to Your Answer? reverse23 q id1 10/15/20 For Loops 5 r p id1 id1 list 5 7 0 1 reverse23

10/15/20 For Loops 24

Questions?