61A Lecture 13 Wednesday, October 2 Announcements 2 Announcements - - PowerPoint PPT Presentation

61a lecture 13
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 13 Wednesday, October 2 Announcements 2 Announcements - - PowerPoint PPT Presentation

61A Lecture 13 Wednesday, October 2 Announcements 2 Announcements Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm. 2 Announcements Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm. Optional Hog strategy contest due


slide-1
SLIDE 1

61A Lecture 13

Wednesday, October 2

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

Announcements

  • Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.

2

slide-4
SLIDE 4

Announcements

  • Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.
  • Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.

2

slide-5
SLIDE 5

Announcements

  • Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.
  • Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.
  • Homework 4 due Tuesday 10/8 @ 11:59pm.

2

slide-6
SLIDE 6

Announcements

  • Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.
  • Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.
  • Homework 4 due Tuesday 10/8 @ 11:59pm.
  • Project 2 due Thursday 10/10 @ 11:59pm.

2

slide-7
SLIDE 7

Announcements

  • Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.
  • Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.
  • Homework 4 due Tuesday 10/8 @ 11:59pm.
  • Project 2 due Thursday 10/10 @ 11:59pm.
  • Guerrilla Section 2 this Saturday 10/5 & Sunday 10/6 10am-1pm in Soda.

2

slide-8
SLIDE 8

Announcements

  • Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.
  • Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.
  • Homework 4 due Tuesday 10/8 @ 11:59pm.
  • Project 2 due Thursday 10/10 @ 11:59pm.
  • Guerrilla Section 2 this Saturday 10/5 & Sunday 10/6 10am-1pm in Soda.
  • Topics: Data abstraction, sequences, and non-local assignment.

2

slide-9
SLIDE 9

Announcements

  • Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.
  • Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.
  • Homework 4 due Tuesday 10/8 @ 11:59pm.
  • Project 2 due Thursday 10/10 @ 11:59pm.
  • Guerrilla Section 2 this Saturday 10/5 & Sunday 10/6 10am-1pm in Soda.
  • Topics: Data abstraction, sequences, and non-local assignment.
  • Please RSVP on Piazza!

2

slide-10
SLIDE 10

Announcements

  • Homework 3 deadline extended to Wednesday 10/2 @ 11:59pm.
  • Optional Hog strategy contest due Thursday 10/3 @ 11:59pm.
  • Homework 4 due Tuesday 10/8 @ 11:59pm.
  • Project 2 due Thursday 10/10 @ 11:59pm.
  • Guerrilla Section 2 this Saturday 10/5 & Sunday 10/6 10am-1pm in Soda.
  • Topics: Data abstraction, sequences, and non-local assignment.
  • Please RSVP on Piazza!
  • Guest lecture on Wednesday 10/9, Peter Norvig on Natural Language Processing in Python.

2

slide-11
SLIDE 11

Strings

slide-12
SLIDE 12

Strings are an Abstraction

4

slide-13
SLIDE 13

Strings are an Abstraction

Representing data: '200' '1.2e-5' 'False' '(1, 2)'

4

slide-14
SLIDE 14

Strings are an Abstraction

Representing data: '200' '1.2e-5' 'False' '(1, 2)' Representing language: """And, as imagination bodies forth The forms of things to unknown, and the poet's pen Turns them to shapes, and gives to airy nothing A local habitation and a name. """

4

slide-15
SLIDE 15

Strings are an Abstraction

Representing data: '200' '1.2e-5' 'False' '(1, 2)' Representing language: """And, as imagination bodies forth The forms of things to unknown, and the poet's pen Turns them to shapes, and gives to airy nothing A local habitation and a name. """ Representing programs: 'curry = lambda f: lambda x: lambda y: f(x, y)'

4

slide-16
SLIDE 16

Strings are an Abstraction

Representing data: '200' '1.2e-5' 'False' '(1, 2)' Representing language: """And, as imagination bodies forth The forms of things to unknown, and the poet's pen Turns them to shapes, and gives to airy nothing A local habitation and a name. """ Representing programs: 'curry = lambda f: lambda x: lambda y: f(x, y)' (Demo)

4

slide-17
SLIDE 17

String Literals Have Three Forms

>>> 'I am string!' 'I am string!' >>> "I've got an apostrophe" "I've got an apostrophe" >>> '您好' '您好'

5

slide-18
SLIDE 18

String Literals Have Three Forms

>>> 'I am string!' 'I am string!' >>> "I've got an apostrophe" "I've got an apostrophe" >>> '您好' '您好' Single-quoted and double-quoted strings are equivalent

5

slide-19
SLIDE 19

String Literals Have Three Forms

>>> 'I am string!' 'I am string!' >>> "I've got an apostrophe" "I've got an apostrophe" >>> '您好' '您好' >>> """The Zen of Python claims, Readability counts. Read more: import this.""" 'The Zen of Python\nclaims, Readability counts.\nRead more: import this.' Single-quoted and double-quoted strings are equivalent

5

slide-20
SLIDE 20

String Literals Have Three Forms

>>> 'I am string!' 'I am string!' >>> "I've got an apostrophe" "I've got an apostrophe" >>> '您好' '您好' >>> """The Zen of Python claims, Readability counts. Read more: import this.""" 'The Zen of Python\nclaims, Readability counts.\nRead more: import this.' A backslash "escapes" the following character Single-quoted and double-quoted strings are equivalent

5

slide-21
SLIDE 21

String Literals Have Three Forms

>>> 'I am string!' 'I am string!' >>> "I've got an apostrophe" "I've got an apostrophe" >>> '您好' '您好' >>> """The Zen of Python claims, Readability counts. Read more: import this.""" 'The Zen of Python\nclaims, Readability counts.\nRead more: import this.' "Line feed" character represents a new line A backslash "escapes" the following character Single-quoted and double-quoted strings are equivalent

5

slide-22
SLIDE 22

Strings are Sequences

6

slide-23
SLIDE 23

Strings are Sequences

  • Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non- negative integer index less than its length, starting at 0 for the first element.

6

slide-24
SLIDE 24

Strings are Sequences

>>> city = 'Berkeley' >>> len(city) 8 >>> city[3] 'k'

  • Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non- negative integer index less than its length, starting at 0 for the first element.

6

slide-25
SLIDE 25

Strings are Sequences

>>> city = 'Berkeley' >>> len(city) 8 >>> city[3] 'k'

  • Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non- negative integer index less than its length, starting at 0 for the first element. An element of a string is itself a string, but with only one character!

6

slide-26
SLIDE 26

Strings are Sequences

>>> city = 'Berkeley' >>> len(city) 8 >>> city[3] 'k'

  • Length. A sequence has a finite length.

Element selection. A sequence has an element corresponding to any non- negative integer index less than its length, starting at 0 for the first element. An element of a string is itself a string, but with only one character!

6

(Demo)

slide-27
SLIDE 27

String Membership Differs from Other Sequence Types

7

slide-28
SLIDE 28

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings

7

slide-29
SLIDE 29

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True >>> 234 in (1, 2, 3, 4, 5) False

7

slide-30
SLIDE 30

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True >>> 234 in (1, 2, 3, 4, 5) False Why? Working with strings, we usually care about words more than characters

7

slide-31
SLIDE 31

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True >>> 234 in (1, 2, 3, 4, 5) False The count method also matches substrings Why? Working with strings, we usually care about words more than characters

7

slide-32
SLIDE 32

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True >>> 234 in (1, 2, 3, 4, 5) False >>> 'Mississippi'.count('i') 4 >>> 'Mississippi'.count('issi') 1 The count method also matches substrings Why? Working with strings, we usually care about words more than characters

7

slide-33
SLIDE 33

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True >>> 234 in (1, 2, 3, 4, 5) False >>> 'Mississippi'.count('i') 4 >>> 'Mississippi'.count('issi') 1 The count method also matches substrings the number of non-overlapping

  • ccurrences of a

substring Why? Working with strings, we usually care about words more than characters

7

slide-34
SLIDE 34

String Membership Differs from Other Sequence Types

The "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True >>> 234 in (1, 2, 3, 4, 5) False >>> 'Mississippi'.count('i') 4 >>> 'Mississippi'.count('issi') 1 The count method also matches substrings the number of non-overlapping

  • ccurrences of a

substring Why? Working with strings, we usually care about words more than characters

7

slide-35
SLIDE 35

Encoding Strings

slide-36
SLIDE 36

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange

9

slide-37
SLIDE 37

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits

9

slide-38
SLIDE 38

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

9

slide-39
SLIDE 39

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code

9

slide-40
SLIDE 40

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset

9

slide-41
SLIDE 41

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

9

slide-42
SLIDE 42

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

"Line feed" (\n)

9

slide-43
SLIDE 43

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

"Line feed" (\n) "Bell" (\a)

9

slide-44
SLIDE 44

Representing Strings: the ASCII Standard

American Standard Code for Information Interchange 8 rows: 3 bits 16 columns: 4 bits

  • Layout was chosen to support sorting by character code
  • Rows indexed 2-5 are a useful 6-bit (64 element) subset
  • Control characters were designed for transmission

"Line feed" (\n) "Bell" (\a)

9

(Demo)

slide-45
SLIDE 45

Representing Strings: the Unicode Standard

10

slide-46
SLIDE 46

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

10

slide-47
SLIDE 47

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters

10

slide-48
SLIDE 48

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)

10

slide-49
SLIDE 49

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

10

slide-50
SLIDE 50

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order

10

slide-51
SLIDE 51

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

10

slide-52
SLIDE 52

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X

10

slide-53
SLIDE 53

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE

10

slide-54
SLIDE 54

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE

10

slide-55
SLIDE 55

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE

'☺'

10

slide-56
SLIDE 56

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE

'☺' '☹'

10

slide-57
SLIDE 57

Representing Strings: the Unicode Standard

http://ian-albert.com/unicode_chart/unichart-chinese.jpg

  • 109,000 characters
  • 93 scripts (organized)
  • Enumeration of character properties,

such as case

  • Supports bidirectional display order
  • A canonical name for every character

U+0058 LATIN CAPITAL LETTER X U+263a WHITE SMILING FACE U+2639 WHITE FROWNING FACE

'☺' '☹'

10

(Demo)

slide-58
SLIDE 58

Representing Strings: UTF-8 Encoding

11

slide-59
SLIDE 59

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

11

slide-60
SLIDE 60

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers

11

slide-61
SLIDE 61

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes

11

slide-62
SLIDE 62

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes A byte is 8 bits and can encode any integer 0-255.

11

slide-63
SLIDE 63

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes A byte is 8 bits and can encode any integer 0-255. bytes

11

slide-64
SLIDE 64

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes A byte is 8 bits and can encode any integer 0-255. bytes integers

11

slide-65
SLIDE 65

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes A byte is 8 bits and can encode any integer 0-255. 00000000 bytes integers

11

slide-66
SLIDE 66

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes A byte is 8 bits and can encode any integer 0-255. 00000000 00000001 1 bytes integers

11

slide-67
SLIDE 67

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes A byte is 8 bits and can encode any integer 0-255. 00000000 00000001 1 00000010 2 bytes integers

11

slide-68
SLIDE 68

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes A byte is 8 bits and can encode any integer 0-255. 00000000 00000001 1 00000011 3 00000010 2 bytes integers

11

slide-69
SLIDE 69

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes A byte is 8 bits and can encode any integer 0-255. Variable-length encoding: integers vary in the number of bytes required to encode them. 00000000 00000001 1 00000011 3 00000010 2 bytes integers

11

slide-70
SLIDE 70

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes A byte is 8 bits and can encode any integer 0-255. Variable-length encoding: integers vary in the number of bytes required to encode them. 00000000 00000001 1 00000011 3 00000010 2 bytes integers In Python: string length is measured in characters, bytes length in bytes.

11

slide-71
SLIDE 71

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format) Unicode: Correspondence between characters and integers UTF-8: Correspondence between those integers and bytes A byte is 8 bits and can encode any integer 0-255. Variable-length encoding: integers vary in the number of bytes required to encode them. 00000000 00000001 1 00000011 3 00000010 2 bytes integers In Python: string length is measured in characters, bytes length in bytes.

11

(Demo)

slide-72
SLIDE 72

Sequence Processing

slide-73
SLIDE 73

Sequence Processing

13

slide-74
SLIDE 74

Consider two problems:

Sequence Processing

13

slide-75
SLIDE 75

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.

Sequence Processing

13

slide-76
SLIDE 76

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

13

slide-77
SLIDE 77

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

13

slide-78
SLIDE 78

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

enumerate naturals:

13

slide-79
SLIDE 79

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. enumerate naturals:

13

slide-80
SLIDE 80

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. enumerate naturals: map fib:

13

slide-81
SLIDE 81

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. enumerate naturals: map fib:

13

slide-82
SLIDE 82

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. enumerate naturals: map fib: filter even:

13

slide-83
SLIDE 83

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. enumerate naturals: map fib: filter even:

13

slide-84
SLIDE 84

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. 0, 2, 8, 34, . enumerate naturals: map fib: filter even:

13

slide-85
SLIDE 85

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. 0, 2, 8, 34, . enumerate naturals: map fib: filter even: accumulate sum:

13

slide-86
SLIDE 86

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

Sequence Processing

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. 0, 2, 8, 34, . enumerate naturals: map fib: filter even: accumulate sum: ., ., ., ., =44

13

slide-87
SLIDE 87

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

14

slide-88
SLIDE 88

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

14

slide-89
SLIDE 89

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word. enumerate words:

14

slide-90
SLIDE 90

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

'University', 'of', 'California', 'Berkeley'

enumerate words:

14

slide-91
SLIDE 91

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

'University', 'of', 'California', 'Berkeley'

enumerate words: filter capitalized:

14

slide-92
SLIDE 92

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

'University', 'of', 'California', 'Berkeley'

enumerate words: filter capitalized:

14

slide-93
SLIDE 93

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

'University', 'of', 'California', 'Berkeley'

enumerate words: filter capitalized:

'University', 'California', 'Berkeley'

14

slide-94
SLIDE 94

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

'University', 'of', 'California', 'Berkeley'

enumerate words: filter capitalized: map first:

'University', 'California', 'Berkeley'

14

slide-95
SLIDE 95

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

'University', 'of', 'California', 'Berkeley'

enumerate words: filter capitalized: map first:

'University', 'California', 'Berkeley' 'U', 'C', 'B'

14

slide-96
SLIDE 96

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

'University', 'of', 'California', 'Berkeley'

enumerate words: filter capitalized: map first: accumulate tuple:

'University', 'California', 'Berkeley' 'U', 'C', 'B'

14

slide-97
SLIDE 97

Sequence Processing

Consider two problems:

  • Sum the even members of the first n Fibonacci numbers.
  • List the letters in the acronym for a name, which includes the first letter of each

capitalized word.

'University', 'of', 'California', 'Berkeley'

enumerate words: filter capitalized: map first: accumulate tuple:

'University', 'California', 'Berkeley' 'U', 'C', 'B' ( 'U', 'C', 'B' )

14

slide-98
SLIDE 98

Mapping a Function over a Sequence

Apply a function to each element of the sequence

15

slide-99
SLIDE 99

Mapping a Function over a Sequence

>>> alternates = (-1, 2, -3, 4, -5) Apply a function to each element of the sequence

15

slide-100
SLIDE 100

Mapping a Function over a Sequence

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) Apply a function to each element of the sequence

15

slide-101
SLIDE 101

Mapping a Function over a Sequence

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence

15

slide-102
SLIDE 102

Mapping a Function over a Sequence

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence The returned value of map is an iterable map object

15

slide-103
SLIDE 103

Mapping a Function over a Sequence

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence The returned value of map is an iterable map object A constructor for the built-in map type

15

slide-104
SLIDE 104

Mapping a Function over a Sequence

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence The returned value of map is an iterable map object A constructor for the built-in map type The returned value of filter is an iterable filter object

15

slide-105
SLIDE 105

Mapping a Function over a Sequence

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence The returned value of map is an iterable map object A constructor for the built-in map type The returned value of filter is an iterable filter object

15

(Demo)

slide-106
SLIDE 106

Iteration and Accumulation

slide-107
SLIDE 107

Iterable Values and Accumulation

17

slide-108
SLIDE 108

Iterable Values and Accumulation

Iterable objects give access to their elements in order.

17

slide-109
SLIDE 109

Iterable Values and Accumulation

Iterable objects give access to their elements in order. Similar to a sequence, but does not always allow element selection or have finite length.

17

slide-110
SLIDE 110

Iterable Values and Accumulation

Iterable objects give access to their elements in order. Similar to a sequence, but does not always allow element selection or have finite length. Many built-in functions take iterable objects as argument.

17

slide-111
SLIDE 111

Iterable Values and Accumulation

Iterable objects give access to their elements in order. Similar to a sequence, but does not always allow element selection or have finite length. Many built-in functions take iterable objects as argument. tuple Return a tuple containing the elements

17

slide-112
SLIDE 112

Iterable Values and Accumulation

Iterable objects give access to their elements in order. Similar to a sequence, but does not always allow element selection or have finite length. Many built-in functions take iterable objects as argument. tuple Return a tuple containing the elements sum Return the sum of the elements

17

slide-113
SLIDE 113

Iterable Values and Accumulation

Iterable objects give access to their elements in order. Similar to a sequence, but does not always allow element selection or have finite length. Many built-in functions take iterable objects as argument. tuple Return a tuple containing the elements sum Return the sum of the elements min Return the minimum of the elements

17

slide-114
SLIDE 114

Iterable Values and Accumulation

Iterable objects give access to their elements in order. Similar to a sequence, but does not always allow element selection or have finite length. Many built-in functions take iterable objects as argument. tuple Return a tuple containing the elements sum Return the sum of the elements min Return the minimum of the elements max Return the maximum of the elements

17

slide-115
SLIDE 115

Iterable Values and Accumulation

Iterable objects give access to their elements in order. Similar to a sequence, but does not always allow element selection or have finite length. Many built-in functions take iterable objects as argument. tuple Return a tuple containing the elements sum Return the sum of the elements min Return the minimum of the elements max Return the maximum of the elements For statements also operate on iterable values.

17

slide-116
SLIDE 116

Reducing a Sequence

18

slide-117
SLIDE 117

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum.

18

slide-118
SLIDE 118

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum. >>> from operator import mul

18

slide-119
SLIDE 119

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum. >>> from operator import mul >>> from functools import reduce

18

slide-120
SLIDE 120

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum. >>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5))

18

slide-121
SLIDE 121

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum. >>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120

18

slide-122
SLIDE 122

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum. >>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120 First argument: A two-argument function

18

slide-123
SLIDE 123

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum. >>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120 First argument: A two-argument function Second argument: an iterable object

18

slide-124
SLIDE 124

Reducing a Sequence

Reduce is a higher-order generalization of max, min, & sum. >>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120 Similar to accumulate from Homework 2, but with iterable objects. First argument: A two-argument function Second argument: an iterable object

18

slide-125
SLIDE 125

Generator Expressions

One large expression that evaluates to an iterable object

19

slide-126
SLIDE 126

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>) One large expression that evaluates to an iterable object

19

slide-127
SLIDE 127

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>) One large expression that evaluates to an iterable object

  • Evaluates to an iterable object.

19

slide-128
SLIDE 128

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>) One large expression that evaluates to an iterable object

  • Evaluates to an iterable object.
  • <iter exp> is evaluated when the generator expression is evaluated.

19

slide-129
SLIDE 129

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>) One large expression that evaluates to an iterable object

  • Evaluates to an iterable object.
  • <iter exp> is evaluated when the generator expression is evaluated.
  • Remaining expressions are evaluated when elements are accessed.

19

slide-130
SLIDE 130

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>) One large expression that evaluates to an iterable object Short version: (<map exp> for <name> in <iter exp>)

  • Evaluates to an iterable object.
  • <iter exp> is evaluated when the generator expression is evaluated.
  • Remaining expressions are evaluated when elements are accessed.

19

slide-131
SLIDE 131

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>) One large expression that evaluates to an iterable object Short version: (<map exp> for <name> in <iter exp>)

  • Evaluates to an iterable object.
  • <iter exp> is evaluated when the generator expression is evaluated.
  • Remaining expressions are evaluated when elements are accessed.

19

(Demo)