CS 61A Lecture 10 Friday, February 13 Announcements 2 - - PowerPoint PPT Presentation

cs 61a lecture 10
SMART_READER_LITE
LIVE PREVIEW

CS 61A Lecture 10 Friday, February 13 Announcements 2 - - PowerPoint PPT Presentation

CS 61A Lecture 10 Friday, February 13 Announcements 2 Announcements Guerrilla Section 2 is on Monday 2/16 2 Announcements Guerrilla Section 2 is on Monday 2/16 RSVP on Piazza if you want to come! 2 Announcements Guerrilla


slide-1
SLIDE 1

CS 61A Lecture 10

Friday, February 13

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

Announcements

  • Guerrilla Section 2 is on Monday 2/16

2

slide-4
SLIDE 4

Announcements

  • Guerrilla Section 2 is on Monday 2/16

§RSVP on Piazza if you want to come!

2

slide-5
SLIDE 5

Announcements

  • Guerrilla Section 2 is on Monday 2/16

§RSVP on Piazza if you want to come!

  • Homework 3 due Thursday 2/19 @ 11:59pm (extended)

2

slide-6
SLIDE 6

Announcements

  • Guerrilla Section 2 is on Monday 2/16

§RSVP on Piazza if you want to come!

  • Homework 3 due Thursday 2/19 @ 11:59pm (extended)

§Homework Party on Tuesday 2/17 5pm-6:30pm in 2050 VLSB

2

slide-7
SLIDE 7

Announcements

  • Guerrilla Section 2 is on Monday 2/16

§RSVP on Piazza if you want to come!

  • Homework 3 due Thursday 2/19 @ 11:59pm (extended)

§Homework Party on Tuesday 2/17 5pm-6:30pm in 2050 VLSB

  • Optional Hog Contest due Wednesday 2/18 @ 11:59pm

2

slide-8
SLIDE 8

Sequences

slide-9
SLIDE 9

The Sequence Abstraction

4

slide-10
SLIDE 10

The Sequence Abstraction

red, orange, yellow, green, blue, indigo, violet.

4

slide-11
SLIDE 11

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). red, orange, yellow, green, blue, indigo, violet.

4

slide-12
SLIDE 12

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.

4

slide-13
SLIDE 13

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.

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

4

slide-14
SLIDE 14

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.

  • 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. 0 , 1 , 2 , 3 , 4 , 5 , 6 .

4

slide-15
SLIDE 15

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.

  • 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. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There is built-in syntax associated with this behavior, or we can use functions.

4

slide-16
SLIDE 16

The Sequence Abstraction

There isn't just one sequence class or data abstraction (in Python or in general). The sequence abstraction is a collection of behaviors: red, orange, yellow, green, blue, indigo, violet.

  • 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. 0 , 1 , 2 , 3 , 4 , 5 , 6 . There is built-in syntax associated with this behavior, or we can use functions. A list is a kind of built-in sequence

4

slide-17
SLIDE 17

Lists

['Demo']

slide-18
SLIDE 18

Lists are Sequences

6

slide-19
SLIDE 19

Lists are Sequences

>>> digits = [1, 8, 2, 8]

6

slide-20
SLIDE 20

Lists are Sequences

>>> digits = [1, 8, 2, 8] >>> len(digits) 4

6

slide-21
SLIDE 21

Lists are Sequences

>>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8

6

slide-22
SLIDE 22

Lists are Sequences

>>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8

6

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

slide-23
SLIDE 23

Lists are Sequences

>>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8

6

  • 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. >>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8]

slide-24
SLIDE 24

Lists are Sequences

>>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8

6

  • 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. >>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8] >>> pairs = [[10, 20], [30, 40]]

slide-25
SLIDE 25

Lists are Sequences

>>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8

6

  • 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. >>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8] >>> pairs = [[10, 20], [30, 40]] >>> pairs[1] [30, 40]

slide-26
SLIDE 26

Lists are Sequences

>>> digits = [1, 8, 2, 8] >>> len(digits) 4 >>> digits[3] 8

6

  • 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. >>> [2, 7] + digits * 2 [2, 7, 1, 8, 2, 8, 1, 8, 2, 8] >>> pairs = [[10, 20], [30, 40]] >>> pairs[1] [30, 40] >>> pairs[1][0] 30

slide-27
SLIDE 27

For Statements

(Demo)

slide-28
SLIDE 28

Sequence Iteration

8

slide-29
SLIDE 29

Sequence Iteration

def count(s, value): total = 0 for element in s:

  • if element == value:

total = total + 1 return total

8

slide-30
SLIDE 30

Sequence Iteration

def count(s, value): total = 0 for element in s:

  • if element == value:

total = total + 1 return total Name bound in the first frame

  • f the current environment

(not a new frame)

8

slide-31
SLIDE 31

For Statement Execution Procedure

9

slide-32
SLIDE 32

For Statement Execution Procedure

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

9

slide-33
SLIDE 33

For Statement Execution Procedure

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

  • 1. Evaluate the header <expression>, which must yield an iterable value (a sequence)

9

slide-34
SLIDE 34

For Statement Execution Procedure

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

  • 1. Evaluate the header <expression>, which must yield an iterable value (a sequence)
  • 2. For each element in that sequence, in order:

9

slide-35
SLIDE 35

For Statement Execution Procedure

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

  • 1. Evaluate the header <expression>, which must yield an iterable value (a sequence)
  • 2. For each element in that sequence, in order:
  • A. Bind <name> to that element in the current frame

9

slide-36
SLIDE 36

For Statement Execution Procedure

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

  • 1. Evaluate the header <expression>, which must yield an iterable value (a sequence)
  • 2. For each element in that sequence, in order:
  • A. Bind <name> to that element in the current frame
  • B. Execute the <suite>

9

slide-37
SLIDE 37

Sequence Unpacking in For Statements

10

slide-38
SLIDE 38

Sequence Unpacking in For Statements

>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]

  • >>> same_count = 0

10

slide-39
SLIDE 39

Sequence Unpacking in For Statements

>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]

  • >>> same_count = 0

A sequence of 
 fixed-length sequences

10

slide-40
SLIDE 40

Sequence Unpacking in For Statements

>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]

  • >>> same_count = 0

>>> for x, y in pairs: ... if x == y: ... same_count = same_count + 1

  • >>> same_count

2 A sequence of 
 fixed-length sequences

10

slide-41
SLIDE 41

Sequence Unpacking in For Statements

>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]

  • >>> same_count = 0

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

10

slide-42
SLIDE 42

Sequence Unpacking in For Statements

>>> pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]

  • >>> same_count = 0

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

10

slide-43
SLIDE 43

Ranges

slide-44
SLIDE 44

The Range Type

A range is a sequence of consecutive integers.*

12

slide-45
SLIDE 45

The Range Type

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

12

slide-46
SLIDE 46

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

The Range Type

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

12

slide-47
SLIDE 47

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

The Range Type

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

range(-2, 2)

12

slide-48
SLIDE 48

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

The Range Type

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

range(-2, 2)

12

slide-49
SLIDE 49

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

The Range Type

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

range(-2, 2)

12

slide-50
SLIDE 50

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

The Range Type

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

range(-2, 2)

12

slide-51
SLIDE 51

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

The Range Type

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

range(-2, 2) Length: ending value - starting value

12

slide-52
SLIDE 52

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

The Range Type

A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

range(-2, 2) Length: ending value - starting value Element selection: starting value + index

12

slide-53
SLIDE 53

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

The Range Type

>>> list(range(-2, 2)) [-2, -1, 0, 1]

  • >>> list(range(4))

[0, 1, 2, 3] A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

range(-2, 2) Length: ending value - starting value Element selection: starting value + index

12

slide-54
SLIDE 54

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

The Range Type

>>> list(range(-2, 2)) [-2, -1, 0, 1]

  • >>> list(range(4))

[0, 1, 2, 3] A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

range(-2, 2) Length: ending value - starting value Element selection: starting value + index List constructor

12

slide-55
SLIDE 55

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

The Range Type

>>> list(range(-2, 2)) [-2, -1, 0, 1]

  • >>> list(range(4))

[0, 1, 2, 3] A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

range(-2, 2) Length: ending value - starting value Element selection: starting value + index List constructor Range with a 0 starting value

12

slide-56
SLIDE 56

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

The Range Type

>>> list(range(-2, 2)) [-2, -1, 0, 1]

  • >>> list(range(4))

[0, 1, 2, 3] A range is a sequence of consecutive integers.*

* Ranges can actually represent more general integer sequences.

range(-2, 2) Length: ending value - starting value Element selection: starting value + index List constructor Range with a 0 starting value (Demo)

12

slide-57
SLIDE 57

List Comprehensions

slide-58
SLIDE 58

List Comprehensions

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'm', 'n', 'o', 'p'] >>> [letters[i] for i in [3, 4, 6, 8]]

slide-59
SLIDE 59

List Comprehensions

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'm', 'n', 'o', 'p'] >>> [letters[i] for i in [3, 4, 6, 8]]

['d', 'e', 'm', 'o']

slide-60
SLIDE 60

List Comprehensions

14

slide-61
SLIDE 61

List Comprehensions

[<map exp> for <name> in <iter exp> if <filter exp>]

14

slide-62
SLIDE 62

List Comprehensions

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>]

14

slide-63
SLIDE 63

List Comprehensions

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] A combined expression that evaluates to a list using this evaluation procedure:

14

slide-64
SLIDE 64

List Comprehensions

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] A combined expression that evaluates to a list using this evaluation procedure:

  • 1. Add a new frame with the current frame as its parent

14

slide-65
SLIDE 65

List Comprehensions

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] A combined expression that evaluates to a list using this evaluation procedure:

  • 1. Add a new frame with the current frame as its parent
  • 2. Create an empty result list that is the value of the expression

14

slide-66
SLIDE 66

List Comprehensions

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] A combined expression that evaluates to a list using this evaluation procedure:

  • 1. Add a new frame with the current frame as its parent
  • 2. Create an empty result list that is the value of the expression
  • 3. For each element in the iterable value of <iter exp>:

14

slide-67
SLIDE 67

List Comprehensions

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] A combined expression that evaluates to a list using this evaluation procedure:

  • 1. Add a new frame with the current frame as its parent
  • 2. Create an empty result list that is the value of the expression
  • 3. For each element in the iterable value of <iter exp>:
  • A. Bind <name> to that element in the new frame from step 1

14

slide-68
SLIDE 68

List Comprehensions

[<map exp> for <name> in <iter exp> if <filter exp>] Short version: [<map exp> for <name> in <iter exp>] A combined expression that evaluates to a list using this evaluation procedure:

  • 1. Add a new frame with the current frame as its parent
  • 2. Create an empty result list that is the value of the expression
  • 3. For each element in the iterable value of <iter exp>:
  • A. Bind <name> to that element in the new frame from step 1
  • B. If <filter exp> evaluates to a true value, then add the value of <map exp>

to the result list

14

slide-69
SLIDE 69

Strings

slide-70
SLIDE 70

Strings are an Abstraction

16

slide-71
SLIDE 71

Strings are an Abstraction

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

16

slide-72
SLIDE 72

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

16

slide-73
SLIDE 73

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

16

slide-74
SLIDE 74

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)

16

slide-75
SLIDE 75

String Literals Have Three Forms

>>> 'I am string!' 'I am string!'

  • >>> "I've got an apostrophe"

"I've got an apostrophe"

  • >>> '您好'

'您好'

17

slide-76
SLIDE 76

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

17

slide-77
SLIDE 77

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

17

slide-78
SLIDE 78

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

17

slide-79
SLIDE 79

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

17

slide-80
SLIDE 80

Strings are Sequences

18

slide-81
SLIDE 81

Strings are Sequences

18

Length and element selection are similar to all sequences

slide-82
SLIDE 82

Strings are Sequences

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

18

Length and element selection are similar to all sequences

slide-83
SLIDE 83

Strings are Sequences

>>> city = 'Berkeley' >>> len(city) 8 >>> city[3] 'k' Careful: An element of a string is itself a string, but with only one element!

18

Length and element selection are similar to all sequences

slide-84
SLIDE 84

Strings are Sequences

>>> city = 'Berkeley' >>> len(city) 8 >>> city[3] 'k' Careful: An element of a string is itself a string, but with only one element!

18

However, the "in" and "not in" operators match substrings Length and element selection are similar to all sequences

slide-85
SLIDE 85

Strings are Sequences

>>> city = 'Berkeley' >>> len(city) 8 >>> city[3] 'k' Careful: An element of a string is itself a string, but with only one element!

18

However, the "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True >>> 234 in [1, 2, 3, 4, 5] False >>> [2, 3, 4] in [1, 2, 3, 4, 5] False Length and element selection are similar to all sequences

slide-86
SLIDE 86

Strings are Sequences

>>> city = 'Berkeley' >>> len(city) 8 >>> city[3] 'k' Careful: An element of a string is itself a string, but with only one element!

18

However, the "in" and "not in" operators match substrings >>> 'here' in "Where's Waldo?" True >>> 234 in [1, 2, 3, 4, 5] False >>> [2, 3, 4] in [1, 2, 3, 4, 5] False When working with strings, we usually care about whole words more than letters Length and element selection are similar to all sequences

slide-87
SLIDE 87

Dictionaries

{'Dem': 0}

slide-88
SLIDE 88

Limitations on Dictionaries

20

slide-89
SLIDE 89

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs

20

slide-90
SLIDE 90

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

20

slide-91
SLIDE 91

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be a list or a dictionary (or any mutable type)

20

slide-92
SLIDE 92

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be a list or a dictionary (or any mutable type)
  • Two keys cannot be equal; There can be at most one value for a given key

20

slide-93
SLIDE 93

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be a list or a dictionary (or any mutable type)
  • Two keys cannot be equal; There can be at most one value for a given key

This first restriction is tied to Python's underlying implementation of dictionaries

20

slide-94
SLIDE 94

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be a list or a dictionary (or any mutable type)
  • Two keys cannot be equal; There can be at most one value for a given key

This first restriction is tied to Python's underlying implementation of dictionaries The second restriction is part of the dictionary abstraction

20

slide-95
SLIDE 95

Limitations on Dictionaries

Dictionaries are unordered collections of key-value pairs Dictionary keys do have two restrictions:

  • A key of a dictionary cannot be a list or a dictionary (or any mutable type)
  • Two keys cannot be equal; There can be at most one value for a given key

This first restriction is tied to Python's underlying implementation of dictionaries The second restriction is part of the dictionary abstraction If you want to associate multiple values with a key, store them all in a sequence value

20