61A Lecture 10 Monday, September 17 Sequence Iteration 2 Sequence - - PowerPoint PPT Presentation

61a lecture 10
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 10 Monday, September 17 Sequence Iteration 2 Sequence - - PowerPoint PPT Presentation

Not on Midterm 1 61A Lecture 10 Monday, September 17 Sequence Iteration 2 Sequence Iteration def count(s, value): total = 0 for elem in s: Name bound in the first frame of the current environment if elem == value: total = total + 1


slide-1
SLIDE 1

61A Lecture 10

Monday, September 17

Not on Midterm 1

slide-2
SLIDE 2

Sequence Iteration

2

slide-3
SLIDE 3

Sequence Iteration

2

def count(s, value): total = 0 for elem in s: if elem == value: total = total + 1 return total Name bound in the first frame

  • f the current environment
slide-4
SLIDE 4

For Statement Execution Procedure

3

slide-5
SLIDE 5

For Statement Execution Procedure

3

for <name> in <expression>: <suite>

slide-6
SLIDE 6

For Statement Execution Procedure

3

for <name> in <expression>: <suite>

  • 1. Evaluate the header <expression>, which must yield an

iterable value.

slide-7
SLIDE 7

For Statement Execution Procedure

3

for <name> in <expression>: <suite>

  • 1. Evaluate the header <expression>, which must yield an

iterable value.

  • 2. For each element in that sequence, in order:
slide-8
SLIDE 8

For Statement Execution Procedure

3

for <name> in <expression>: <suite>

  • 1. Evaluate the header <expression>, which must yield an

iterable value.

  • 2. For each element in that sequence, in order:
  • A. Bind <name> to that element in the first frame of the

current environment.

slide-9
SLIDE 9

For Statement Execution Procedure

3

for <name> in <expression>: <suite>

  • 1. Evaluate the header <expression>, which must yield an

iterable value.

  • 2. For each element in that sequence, in order:
  • A. Bind <name> to that element in the first frame of the

current environment.

  • B. Execute the <suite>.
slide-10
SLIDE 10

Sequence Unpacking in For Statements

4

slide-11
SLIDE 11

Sequence Unpacking in For Statements

>>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0

4

slide-12
SLIDE 12

Sequence Unpacking in For Statements

>>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0

4

A sequence of fixed-length sequences

slide-13
SLIDE 13

Sequence Unpacking in For Statements

>>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0

4

>>> for x, y in pairs: if x == y: same_count = same_count + 1 >>> same_count 2 A sequence of fixed-length sequences

slide-14
SLIDE 14

Sequence Unpacking in For Statements

>>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0

4

>>> for x, y in pairs: if x == y: same_count = same_count + 1 >>> same_count 2 A sequence of fixed-length sequences A name for each element in a fixed-length sequence

slide-15
SLIDE 15

Sequence Unpacking in For Statements

>>> pairs = ((1, 2), (2, 2), (2, 3), (4, 4)) >>> same_count = 0

4

>>> for x, y in pairs: if x == y: same_count = same_count + 1 >>> same_count 2 A sequence of fixed-length sequences A name for each element in a fixed-length sequence Each name is bound to a value, as in multiple assignment

slide-16
SLIDE 16

The Range Type

5

A range is a sequence of consecutive integers.*

slide-17
SLIDE 17

The Range Type

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

slide-18
SLIDE 18

The Range Type

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ...

slide-19
SLIDE 19

The Range Type

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2)

slide-20
SLIDE 20

The Range Type

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2)

slide-21
SLIDE 21

The Range Type

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2)

slide-22
SLIDE 22

The Range Type

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2)

slide-23
SLIDE 23

The Range Type

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value

slide-24
SLIDE 24

The Range Type

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Element selection: starting value + index

slide-25
SLIDE 25

The Range Type

>>> tuple(range(-2, 2)) (-2, -1, 0, 1) >>> tuple(range(4)) (0, 1, 2, 3)

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Element selection: starting value + index

slide-26
SLIDE 26

The Range Type

>>> tuple(range(-2, 2)) (-2, -1, 0, 1) >>> tuple(range(4)) (0, 1, 2, 3)

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Element selection: starting value + index Tuple constructor

slide-27
SLIDE 27

The Range Type

>>> tuple(range(-2, 2)) (-2, -1, 0, 1) >>> tuple(range(4)) (0, 1, 2, 3)

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Element selection: starting value + index Tuple constructor With a 0 starting value

slide-28
SLIDE 28

The Range Type

>>> tuple(range(-2, 2)) (-2, -1, 0, 1) >>> tuple(range(4)) (0, 1, 2, 3)

5

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

..., -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, ... range(-2, 2) Length: ending value - starting value Element selection: starting value + index Tuple constructor With a 0 starting value (Demo)

slide-29
SLIDE 29

Membership & Slicing

6

The Python sequence abstraction has two more behaviors!

slide-30
SLIDE 30

Membership & Slicing

6

The Python sequence abstraction has two more behaviors! Membership.

slide-31
SLIDE 31

Membership & Slicing

>>> digits = (1, 8, 2, 8) >>> 2 in digits True >>> 1828 not in digits True

6

The Python sequence abstraction has two more behaviors! Membership.

slide-32
SLIDE 32

Membership & Slicing

>>> digits = (1, 8, 2, 8) >>> 2 in digits True >>> 1828 not in digits True

6

The Python sequence abstraction has two more behaviors! Membership. Slicing.

slide-33
SLIDE 33

Membership & Slicing

>>> digits = (1, 8, 2, 8) >>> 2 in digits True >>> 1828 not in digits True

6

>>> digits[0:2] (1, 8) >>> digits[1:] (8, 2, 8) The Python sequence abstraction has two more behaviors! Membership. Slicing.

slide-34
SLIDE 34

Strings are an Abstraction

7

slide-35
SLIDE 35

Strings are an Abstraction

7

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

slide-36
SLIDE 36

Strings are an Abstraction

7

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. """

slide-37
SLIDE 37

Strings are an Abstraction

7

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)'

slide-38
SLIDE 38

Strings are an Abstraction

7

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

slide-39
SLIDE 39

String Literals Have Three Forms

8

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

slide-40
SLIDE 40

String Literals Have Three Forms

8

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

slide-41
SLIDE 41

String Literals Have Three Forms

8

>>> '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- and double-quoted strings are equivalent

slide-42
SLIDE 42

String Literals Have Three Forms

8

>>> '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- and double-quoted strings are equivalent

slide-43
SLIDE 43

String Literals Have Three Forms

8

>>> '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- and double-quoted strings are equivalent

slide-44
SLIDE 44

Strings are Sequences

9

slide-45
SLIDE 45

Strings are Sequences

9

  • 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.

slide-46
SLIDE 46

Strings are Sequences

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

9

  • 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.

slide-47
SLIDE 47

Strings are Sequences

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

9

  • 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!

slide-48
SLIDE 48

Strings are Sequences

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

9

  • 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! >>> 'Berkeley' + ', CA' 'Berkeley, CA' >>> 'Shabu ' * 2 'Shabu Shabu '

slide-49
SLIDE 49

Strings are Sequences

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

9

  • 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! >>> 'Berkeley' + ', CA' 'Berkeley, CA' >>> 'Shabu ' * 2 'Shabu Shabu ' String arithmetic is similar to tuple arithmetic

slide-50
SLIDE 50

String Membership Differs from Other Sequence Types

10

slide-51
SLIDE 51

String Membership Differs from Other Sequence Types

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

10

slide-52
SLIDE 52

String Membership Differs from Other Sequence Types

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

10

>>> 'here' in "Where's Waldo?" True

slide-53
SLIDE 53

String Membership Differs from Other Sequence Types

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

10

>>> 'here' in "Where's Waldo?" True Why? Working with strings, we care about words, not characters

slide-54
SLIDE 54

String Membership Differs from Other Sequence Types

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

10

>>> 'here' in "Where's Waldo?" True The count method also matches substrings Why? Working with strings, we care about words, not characters

slide-55
SLIDE 55

String Membership Differs from Other Sequence Types

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

10

>>> 'here' in "Where's Waldo?" True >>> 'Mississippi'.count('i') 4 >>> 'Mississippi'.count('issi') 1 The count method also matches substrings Why? Working with strings, we care about words, not characters

slide-56
SLIDE 56

String Membership Differs from Other Sequence Types

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

10

>>> 'here' in "Where's Waldo?" True >>> '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 care about words, not characters

slide-57
SLIDE 57

String Membership Differs from Other Sequence Types

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

10

>>> 'here' in "Where's Waldo?" True >>> '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 care about words, not characters

slide-58
SLIDE 58

Representing Strings: the ASCII Standard

11

American Standard Code for Information Interchange Bonus Material

slide-59
SLIDE 59

Representing Strings: the ASCII Standard

11

American Standard Code for Information Interchange 8 rows: 3 bits Bonus Material

slide-60
SLIDE 60

Representing Strings: the ASCII Standard

11

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

slide-61
SLIDE 61

Representing Strings: the ASCII Standard

11

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

  • Layout was chosen to support sorting by character code

Bonus Material

slide-62
SLIDE 62

Representing Strings: the ASCII Standard

11

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

Bonus Material

slide-63
SLIDE 63

Representing Strings: the ASCII Standard

11

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

Bonus Material

slide-64
SLIDE 64

Representing Strings: the ASCII Standard

11

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" Bonus Material

slide-65
SLIDE 65

Representing Strings: the ASCII Standard

11

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" "Bell" Bonus Material

slide-66
SLIDE 66

Representing Strings: the ASCII Standard

11

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" "Bell" Demo Bonus Material

slide-67
SLIDE 67

Representing Strings: the Unicode Standard

12

Bonus Material

slide-68
SLIDE 68

Representing Strings: the Unicode Standard

12

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

Bonus Material

slide-69
SLIDE 69

Representing Strings: the Unicode Standard

12

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

  • 109,000 characters

Bonus Material

slide-70
SLIDE 70

Representing Strings: the Unicode Standard

12

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

  • 109,000 characters
  • 93 scripts (organized)

Bonus Material

slide-71
SLIDE 71

Representing Strings: the Unicode Standard

12

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

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

properties, such as case Bonus Material

slide-72
SLIDE 72

Representing Strings: the Unicode Standard

12

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 Bonus Material

slide-73
SLIDE 73

Representing Strings: the Unicode Standard

12

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 Bonus Material

slide-74
SLIDE 74

Representing Strings: the Unicode Standard

12

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 Bonus Material

slide-75
SLIDE 75

Representing Strings: the Unicode Standard

12

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 Bonus Material

slide-76
SLIDE 76

Representing Strings: the Unicode Standard

12

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 Bonus Material

slide-77
SLIDE 77

Representing Strings: the Unicode Standard

12

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

'☺'

Bonus Material

slide-78
SLIDE 78

Representing Strings: the Unicode Standard

12

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

'☺' '☹'

Bonus Material

slide-79
SLIDE 79

Representing Strings: the Unicode Standard

12

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

'☺' '☹'

Demo Bonus Material

slide-80
SLIDE 80

Representing Strings: UTF-8 Encoding

13

Bonus Material

slide-81
SLIDE 81

Representing Strings: UTF-8 Encoding

UTF (UCS (Universal Character Set) Transformation Format)

13

Bonus Material

slide-82
SLIDE 82

Representing Strings: UTF-8 Encoding

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

13

Bonus Material

slide-83
SLIDE 83

Representing Strings: UTF-8 Encoding

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

13

Bonus Material

slide-84
SLIDE 84

Representing Strings: UTF-8 Encoding

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

13

Bonus Material

slide-85
SLIDE 85

Representing Strings: UTF-8 Encoding

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

13

bytes Bonus Material

slide-86
SLIDE 86

Representing Strings: UTF-8 Encoding

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

13

bytes integers Bonus Material

slide-87
SLIDE 87

Representing Strings: UTF-8 Encoding

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

13

00000000 bytes integers Bonus Material

slide-88
SLIDE 88

Representing Strings: UTF-8 Encoding

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

13

00000000 00000001 1 bytes integers Bonus Material

slide-89
SLIDE 89

Representing Strings: UTF-8 Encoding

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

13

00000000 00000001 1 00000010 2 bytes integers Bonus Material

slide-90
SLIDE 90

Representing Strings: UTF-8 Encoding

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

13

00000000 00000001 1 00000011 3 00000010 2 bytes integers Bonus Material

slide-91
SLIDE 91

Representing Strings: UTF-8 Encoding

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

13

Variable-length encoding: integers vary in the number of bytes required to encode them! 00000000 00000001 1 00000011 3 00000010 2 bytes integers Bonus Material

slide-92
SLIDE 92

Representing Strings: UTF-8 Encoding

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

13

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 in characters, bytes length in bytes Bonus Material

slide-93
SLIDE 93

Representing Strings: UTF-8 Encoding

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

13

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 in characters, bytes length in bytes Demo Bonus Material

slide-94
SLIDE 94

Sequences as Conventional Interfaces

14

slide-95
SLIDE 95

Sequences as Conventional Interfaces

Consider two problems:

14

slide-96
SLIDE 96

Sequences as Conventional Interfaces

Consider two problems:

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

14

slide-97
SLIDE 97

Sequences as Conventional Interfaces

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-98
SLIDE 98

Sequences as Conventional Interfaces

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-99
SLIDE 99

Sequences as Conventional Interfaces

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

enumerate naturals:

slide-100
SLIDE 100

Sequences as Conventional Interfaces

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

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

slide-101
SLIDE 101

Sequences as Conventional Interfaces

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

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

slide-102
SLIDE 102

Sequences as Conventional Interfaces

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

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:

slide-103
SLIDE 103

Sequences as Conventional Interfaces

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

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 iseven:

slide-104
SLIDE 104

Sequences as Conventional Interfaces

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

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 iseven:

slide-105
SLIDE 105

Sequences as Conventional Interfaces

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

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 iseven:

slide-106
SLIDE 106

Sequences as Conventional Interfaces

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

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 iseven: accumulate sum:

slide-107
SLIDE 107

Sequences as Conventional Interfaces

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

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 iseven: accumulate sum: ., ., ., ., 44.

slide-108
SLIDE 108

Sequences as Conventional Interfaces

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.

15

slide-109
SLIDE 109

Sequences as Conventional Interfaces

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.

15

slide-110
SLIDE 110

Sequences as Conventional Interfaces

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.

15

enumerate words:

slide-111
SLIDE 111

Sequences as Conventional Interfaces

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.

15

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

enumerate words:

slide-112
SLIDE 112

Sequences as Conventional Interfaces

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.

15

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

enumerate words: filter iscap:

slide-113
SLIDE 113

Sequences as Conventional Interfaces

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.

15

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

enumerate words: filter iscap:

slide-114
SLIDE 114

Sequences as Conventional Interfaces

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.

15

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

enumerate words: filter iscap:

'University', 'California', 'Berkeley'

slide-115
SLIDE 115

Sequences as Conventional Interfaces

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.

15

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

enumerate words: filter iscap: map first:

'University', 'California', 'Berkeley'

slide-116
SLIDE 116

Sequences as Conventional Interfaces

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.

15

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

enumerate words: filter iscap: map first:

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

slide-117
SLIDE 117

Sequences as Conventional Interfaces

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.

15

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

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

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

slide-118
SLIDE 118

Sequences as Conventional Interfaces

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.

15

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

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

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

slide-119
SLIDE 119

Mapping a Function over a Sequence

16

Apply a function to each element of the sequence

slide-120
SLIDE 120

Mapping a Function over a Sequence

16

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

slide-121
SLIDE 121

Mapping a Function over a Sequence

16

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

slide-122
SLIDE 122

Mapping a Function over a Sequence

16

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

slide-123
SLIDE 123

Mapping a Function over a Sequence

16

>>> 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

slide-124
SLIDE 124

Mapping a Function over a Sequence

16

>>> 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

slide-125
SLIDE 125

Mapping a Function over a Sequence

16

>>> 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

slide-126
SLIDE 126

Mapping a Function over a Sequence

16

>>> alternates = (-1, 2, -3, 4, -5) >>> tuple(map(abs, alternates)) (1, 2, 3, 4, 5) Apply a function to each element of the sequence Demo 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

slide-127
SLIDE 127

Accumulation and Iterable Values

17

slide-128
SLIDE 128

Accumulation and Iterable Values

Iterable objects give access to some elements in order.

17

slide-129
SLIDE 129

Accumulation and Iterable Values

Iterable objects give access to some elements in order. Python-specific construct; less specific than a sequence

17

slide-130
SLIDE 130

Accumulation and Iterable Values

Iterable objects give access to some elements in order. Python-specific construct; less specific than a sequence Many built-in functions take iterable objects as argument.

17

slide-131
SLIDE 131

Accumulation and Iterable Values

Iterable objects give access to some elements in order. Python-specific construct; less specific than a sequence Many built-in functions take iterable objects as argument.

17

tuple Return a tuple containing the elements

slide-132
SLIDE 132

Accumulation and Iterable Values

Iterable objects give access to some elements in order. Python-specific construct; less specific than a sequence Many built-in functions take iterable objects as argument.

17

tuple Return a tuple containing the elements sum Return the sum of the elements

slide-133
SLIDE 133

Accumulation and Iterable Values

Iterable objects give access to some elements in order. Python-specific construct; less specific than a sequence Many built-in functions take iterable objects as argument.

17

tuple Return a tuple containing the elements sum Return the sum of the elements min Return the minimum of the elements

slide-134
SLIDE 134

Accumulation and Iterable Values

Iterable objects give access to some elements in order. Python-specific construct; less specific than a sequence Many built-in functions take iterable objects as argument.

17

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

slide-135
SLIDE 135

Accumulation and Iterable Values

Iterable objects give access to some elements in order. Python-specific construct; less specific than a sequence Many built-in functions take iterable objects as argument.

17

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.

slide-136
SLIDE 136

Accumulation and Iterable Values

Iterable objects give access to some elements in order. Python-specific construct; less specific than a sequence Many built-in functions take iterable objects as argument.

17

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. Demo

slide-137
SLIDE 137

Generator Expressions

18

One large expression that evaluates to an iterable object

slide-138
SLIDE 138

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

18

One large expression that evaluates to an iterable object

slide-139
SLIDE 139

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

18

One large expression that evaluates to an iterable object

  • Evaluates to an iterable object.
slide-140
SLIDE 140

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

18

One large expression that evaluates to an iterable object

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

is evaluated.

slide-141
SLIDE 141

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

18

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.

slide-142
SLIDE 142

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

18

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.

slide-143
SLIDE 143

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

18

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. Precise evaluation rule introduced in Chapter 4.

slide-144
SLIDE 144

Generator Expressions

(<map exp> for <name> in <iter exp> if <filter exp>)

18

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. Precise evaluation rule introduced in Chapter 4.

Demo

slide-145
SLIDE 145

Reducing a Sequence

19

slide-146
SLIDE 146

Reducing a Sequence

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

19

slide-147
SLIDE 147

Reducing a Sequence

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

19

>>> from operator import mul

slide-148
SLIDE 148

Reducing a Sequence

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

19

>>> from operator import mul >>> from functools import reduce

slide-149
SLIDE 149

Reducing a Sequence

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

19

>>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5))

slide-150
SLIDE 150

Reducing a Sequence

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

19

>>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120

slide-151
SLIDE 151

Reducing a Sequence

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

19

>>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120 First argument: A two-argument function

slide-152
SLIDE 152

Reducing a Sequence

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

19

>>> 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

slide-153
SLIDE 153

Reducing a Sequence

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

19

>>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5)) 120 Like accumulate from Homework 2, but with iterable objects First argument: A two-argument function Second argument: an iterable object