CS61A Lecture 16 Amir Kamil UC Berkeley February 27, 2013 - - PowerPoint PPT Presentation

cs61a lecture 16
SMART_READER_LITE
LIVE PREVIEW

CS61A Lecture 16 Amir Kamil UC Berkeley February 27, 2013 - - PowerPoint PPT Presentation

CS61A Lecture 16 Amir Kamil UC Berkeley February 27, 2013 Announcements HW5 due tonight Trends project due on Tuesday Partners are required; find one in lab or on Piazza Will not work in IDLE New bug submission policy; see


slide-1
SLIDE 1

CS61A Lecture 16

Amir Kamil UC Berkeley February 27, 2013

slide-2
SLIDE 2

 HW5 due tonight  Trends project due on Tuesday

 Partners are required; find one in lab or on Piazza  Will not work in IDLE  New bug submission policy; see Piazza

Announcements

slide-3
SLIDE 3

Iterables

Iterables provide access to some elements in order but do not provide length or element selection Python‐specific construct; more general than a sequence Many built‐in functions take iterables as argument For statements also operate on iterable values.

tuple Construct a tuple containing the elements map Construct a map that results from applying the given function to each element filter Construct a filter with elements that satisfy the given condition sum Return the sum of the elements min Return the minimum of the elements max Return the maximum of the elements

slide-4
SLIDE 4

Generator Expressions

slide-5
SLIDE 5

Generator Expressions

One large expression that combines mapping and filtering to produce an iterable

slide-6
SLIDE 6

Generator Expressions

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

One large expression that combines mapping and filtering to produce an iterable

slide-7
SLIDE 7

Generator Expressions

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

One large expression that combines mapping and filtering to produce an iterable

  • Evaluates to an iterable.
slide-8
SLIDE 8

Generator Expressions

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

One large expression that combines mapping and filtering to produce an iterable

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

evaluated.

slide-9
SLIDE 9

Generator Expressions

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

One large expression that combines mapping and filtering to produce an iterable

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

evaluated.

  • Remaining expressions are evaluated when elements are

accessed.

slide-10
SLIDE 10

Generator Expressions

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

One large expression that combines mapping and filtering to produce an iterable No‐filter version: (<map exp> for <name> in <iter exp>)

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

evaluated.

  • Remaining expressions are evaluated when elements are

accessed.

slide-11
SLIDE 11

Generator Expressions

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

One large expression that combines mapping and filtering to produce an iterable No‐filter version: (<map exp> for <name> in <iter exp>)

  • Evaluates to an iterable.
  • <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-12
SLIDE 12

Reducing a Sequence

slide-13
SLIDE 13

Reducing a Sequence

Reduce is a higher‐order generalization of max, min, and sum.

slide-14
SLIDE 14

Reducing a Sequence

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

slide-15
SLIDE 15

Reducing a Sequence

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

slide-16
SLIDE 16

Reducing a Sequence

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

slide-17
SLIDE 17

Reducing a Sequence

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

slide-18
SLIDE 18

Reducing a Sequence

Reduce is a higher‐order generalization of max, min, and sum. >>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5), 1) 120 Like accumulate from Homework 2, but with iterables First argument: A two‐argument function Second argument: an iterable object Optional initial value as third argument

slide-19
SLIDE 19

Reducing a Sequence

Reduce is a higher‐order generalization of max, min, and sum. >>> from operator import mul >>> from functools import reduce >>> reduce(mul, (1, 2, 3, 4, 5), 1) 120 Like accumulate from Homework 2, but with iterables First argument: A two‐argument function Second argument: an iterable object Optional initial value as third argument

def accumulate(combiner, start, n, term): return reduce(combiner, map(term, range(1, n + 1)), start)

slide-20
SLIDE 20

Create an iterable of fixed‐length sequences

More Functions on Iterables (Bonus)

>>> a, b = (1, 2, 3), (4, 5, 6, 7) >>> for x, y in zip(a, b): ... print(x + y) ... 5 7 9

The itertools module contains many useful functions for working with iterables

>>> from itertools import product, combinations >>> tuple(product(a, b[:2])) ((1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)) >>> tuple(combinations(a, 2)) ((1, 2), (1, 3), (2, 3))

Produces tuples with one element from each argument, up to length

  • f smallest argument
slide-21
SLIDE 21

Lists

slide-22
SLIDE 22

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2]

slide-23
SLIDE 23

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2]

Create a list using square brackets

slide-24
SLIDE 24

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2] >>> len(a) 3 >>> a[1] 1

Create a list using square brackets

slide-25
SLIDE 25

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2] >>> len(a) 3 >>> a[1] 1

Create a list using square brackets Lists are sequences

slide-26
SLIDE 26

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2] >>> len(a) 3 >>> a[1] 1 >>> c, d = a, a[:] >>> a, c, d ([3, 1, 2], [3, 1, 2], [3, 1, 2])

Create a list using square brackets Lists are sequences

slide-27
SLIDE 27

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2] >>> len(a) 3 >>> a[1] 1 >>> c, d = a, a[:] >>> a, c, d ([3, 1, 2], [3, 1, 2], [3, 1, 2])

Create a list using square brackets Lists are sequences Bind another name to a list or a slice of a list

slide-28
SLIDE 28

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2] >>> len(a) 3 >>> a[1] 1 >>> c, d = a, a[:] >>> a, c, d ([3, 1, 2], [3, 1, 2], [3, 1, 2]) >>> c[0] = 4

Create a list using square brackets Lists are sequences Bind another name to a list or a slice of a list

slide-29
SLIDE 29

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2] >>> len(a) 3 >>> a[1] 1 >>> c, d = a, a[:] >>> a, c, d ([3, 1, 2], [3, 1, 2], [3, 1, 2]) >>> c[0] = 4

Create a list using square brackets Lists are sequences Bind another name to a list or a slice of a list Modify contents of a list

slide-30
SLIDE 30

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2] >>> len(a) 3 >>> a[1] 1 >>> c, d = a, a[:] >>> a, c, d ([3, 1, 2], [3, 1, 2], [3, 1, 2]) >>> c[0] = 4 >>> a, c, d ([4, 1, 2], [4, 1, 2], [3, 1, 2])

Create a list using square brackets Lists are sequences Bind another name to a list or a slice of a list Modify contents of a list

slide-31
SLIDE 31

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2] >>> len(a) 3 >>> a[1] 1 >>> c, d = a, a[:] >>> a, c, d ([3, 1, 2], [3, 1, 2], [3, 1, 2]) >>> c[0] = 4 >>> a, c, d ([4, 1, 2], [4, 1, 2], [3, 1, 2]) >>> d[0] = 5 >>> a, c, d ([4, 1, 2], [4, 1, 2], [5, 1, 2])

Create a list using square brackets Lists are sequences Bind another name to a list or a slice of a list Modify contents of a list

slide-32
SLIDE 32

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2] >>> len(a) 3 >>> a[1] 1 >>> c, d = a, a[:] >>> a, c, d ([3, 1, 2], [3, 1, 2], [3, 1, 2]) >>> c[0] = 4 >>> a, c, d ([4, 1, 2], [4, 1, 2], [3, 1, 2]) >>> d[0] = 5 >>> a, c, d ([4, 1, 2], [4, 1, 2], [5, 1, 2]) >>> a[1:2] = [7, 8, 9] >>> a, c, d ([4, 7, 8, 9, 2], [4, 7, 8, 9, 2], [5, 1, 2])

Create a list using square brackets Lists are sequences Bind another name to a list or a slice of a list Modify contents of a list

slide-33
SLIDE 33

Lists

>>> a = [3, 1, 2] >>> a [3, 1, 2] >>> len(a) 3 >>> a[1] 1 >>> c, d = a, a[:] >>> a, c, d ([3, 1, 2], [3, 1, 2], [3, 1, 2]) >>> c[0] = 4 >>> a, c, d ([4, 1, 2], [4, 1, 2], [3, 1, 2]) >>> d[0] = 5 >>> a, c, d ([4, 1, 2], [4, 1, 2], [5, 1, 2]) >>> a[1:2] = [7, 8, 9] >>> a, c, d ([4, 7, 8, 9, 2], [4, 7, 8, 9, 2], [5, 1, 2])

Create a list using square brackets Lists are sequences Bind another name to a list or a slice of a list Modify contents of a list wut()?

slide-34
SLIDE 34

   

Objects

slide-35
SLIDE 35

An object is a representation of information

  

Objects

slide-36
SLIDE 36

An object is a representation of information All data in Python are objects

 

Objects

slide-37
SLIDE 37

An object is a representation of information All data in Python are objects But an object is not just data; it also bundles behavior together with that data

Objects

slide-38
SLIDE 38

An object is a representation of information All data in Python are objects But an object is not just data; it also bundles behavior together with that data An object’s type determines what data it stores and what behavior it provides

Objects

slide-39
SLIDE 39

An object is a representation of information All data in Python are objects But an object is not just data; it also bundles behavior together with that data An object’s type determines what data it stores and what behavior it provides

Objects

>>> type(4) <class 'int'> >>> type([4]) <class 'list'>

slide-40
SLIDE 40

   

Object Attributes

slide-41
SLIDE 41

All objects have attributes

  

Object Attributes

slide-42
SLIDE 42

All objects have attributes We use dot notation to access an attribute

 

Object Attributes

slide-43
SLIDE 43

All objects have attributes We use dot notation to access an attribute

 

Object Attributes

>>> (4).real, (4).imag (4, 0)

slide-44
SLIDE 44

All objects have attributes We use dot notation to access an attribute An attribute may be a method, which is a type of function, so it may be called

Object Attributes

>>> (4).real, (4).imag (4, 0)

slide-45
SLIDE 45

All objects have attributes We use dot notation to access an attribute An attribute may be a method, which is a type of function, so it may be called

Object Attributes

>>> (4).real, (4).imag (4, 0) >>> [1, 2, 1, 4].count(1) 2

slide-46
SLIDE 46

All objects have attributes We use dot notation to access an attribute An attribute may be a method, which is a type of function, so it may be called Notice that we did not have to pass in the list as an argument; the method already knows the object on which it is operating

Object Attributes

>>> (4).real, (4).imag (4, 0) >>> [1, 2, 1, 4].count(1) 2

slide-47
SLIDE 47

   

Creating and Distinguishing Objects

slide-48
SLIDE 48

Calling the constructor of a built‐in type creates a new object of that type

  

Creating and Distinguishing Objects

slide-49
SLIDE 49

Calling the constructor of a built‐in type creates a new object of that type Objects can be distinct even if they hold the same data

 

Creating and Distinguishing Objects

slide-50
SLIDE 50

Calling the constructor of a built‐in type creates a new object of that type Objects can be distinct even if they hold the same data The is and not is operators check if two objects are the same

Creating and Distinguishing Objects

slide-51
SLIDE 51

Calling the constructor of a built‐in type creates a new object of that type Objects can be distinct even if they hold the same data The is and not is operators check if two objects are the same

Creating and Distinguishing Objects

>>> [1, 2, 1, 4] is [1, 2, 1, 4] False

slide-52
SLIDE 52

Calling the constructor of a built‐in type creates a new object of that type Objects can be distinct even if they hold the same data The is and not is operators check if two objects are the same Compare to ==, which checks for equality, not sameness

Creating and Distinguishing Objects

>>> [1, 2, 1, 4] is [1, 2, 1, 4] False

slide-53
SLIDE 53

Calling the constructor of a built‐in type creates a new object of that type Objects can be distinct even if they hold the same data The is and not is operators check if two objects are the same Compare to ==, which checks for equality, not sameness

Creating and Distinguishing Objects

>>> [1, 2, 1, 4] is [1, 2, 1, 4] False >>> [1, 2, 1, 4] == [1, 2, 1, 4] True

slide-54
SLIDE 54

   

Objects and Assignment

Example: http://goo.gl/Xrm4k

slide-55
SLIDE 55

Assignment does not create a new object

  

Objects and Assignment

Example: http://goo.gl/Xrm4k

slide-56
SLIDE 56

Assignment does not create a new object

  

Objects and Assignment

Example: http://goo.gl/Xrm4k

slide-57
SLIDE 57

Assignment does not create a new object

  

Objects and Assignment

Example: http://goo.gl/Xrm4k

But slicing does!

slide-58
SLIDE 58

Assignment does not create a new object In our environment diagrams, assignment copies the arrow

 

Objects and Assignment

Example: http://goo.gl/Xrm4k

But slicing does!

slide-59
SLIDE 59

Assignment does not create a new object In our environment diagrams, assignment copies the arrow The “arrow” is called a pointer or reference

Objects and Assignment

Example: http://goo.gl/Xrm4k

But slicing does!

slide-60
SLIDE 60

Assignment does not create a new object In our environment diagrams, assignment copies the arrow The “arrow” is called a pointer or reference Multiple names can point to or reference the same object

Objects and Assignment

Example: http://goo.gl/Xrm4k

But slicing does!

slide-61
SLIDE 61

 

 

Immutable Types

slide-62
SLIDE 62

An object may be immutable, which means that its data cannot be changed

 

Immutable Types

slide-63
SLIDE 63

An object may be immutable, which means that its data cannot be changed Most of the types we have seen so far are immutable

 ints, floats, booleans, tuples, ranges, strings

 

Immutable Types

slide-64
SLIDE 64

An object may be immutable, which means that its data cannot be changed Most of the types we have seen so far are immutable

 ints, floats, booleans, tuples, ranges, strings

For an immutable type, it doesn’t matter whether or not two equal objects are the same

Immutable Types

slide-65
SLIDE 65

An object may be immutable, which means that its data cannot be changed Most of the types we have seen so far are immutable

 ints, floats, booleans, tuples, ranges, strings

For an immutable type, it doesn’t matter whether or not two equal objects are the same Neither can change, so one is as good as the other

Immutable Types

slide-66
SLIDE 66

An object may be immutable, which means that its data cannot be changed Most of the types we have seen so far are immutable

 ints, floats, booleans, tuples, ranges, strings

For an immutable type, it doesn’t matter whether or not two equal objects are the same Neither can change, so one is as good as the other

Immutable Types

>>> e, f = 1e12, 1e12 >>> e is f True >>> e = 1e12 >>> f = 1e12 >>> e is f False

slide-67
SLIDE 67

 

Mutable Types

Example: http://goo.gl/ornZ8

slide-68
SLIDE 68

Mutable objects, on the other hand, can change, and any change affects all references to that object

Mutable Types

Example: http://goo.gl/ornZ8

slide-69
SLIDE 69

Mutable objects, on the other hand, can change, and any change affects all references to that object

Mutable Types

Example: http://goo.gl/ornZ8

slide-70
SLIDE 70

Mutable objects, on the other hand, can change, and any change affects all references to that object So we need to be careful with mutation

Mutable Types

Example: http://goo.gl/ornZ8

slide-71
SLIDE 71

    

  

List Methods

slide-72
SLIDE 72

Lists have many useful methods

    

  

List Methods

slide-73
SLIDE 73

Lists have many useful methods

 append: add an element to the end of a list  extend: add all elements from an iterable to the end of

the list

 count: count the number of occurrences of a value  pop: remove an element from the end of a list  sort: sort the elements of a list

  

List Methods

slide-74
SLIDE 74

Lists have many useful methods

 append: add an element to the end of a list  extend: add all elements from an iterable to the end of

the list

 count: count the number of occurrences of a value  pop: remove an element from the end of a list  sort: sort the elements of a list

These methods (except count) mutate the list

 

List Methods

slide-75
SLIDE 75

Lists have many useful methods

 append: add an element to the end of a list  extend: add all elements from an iterable to the end of

the list

 count: count the number of occurrences of a value  pop: remove an element from the end of a list  sort: sort the elements of a list

These methods (except count) mutate the list Compare to sorted(x), which returns a new list

List Methods

slide-76
SLIDE 76

Lists have many useful methods

 append: add an element to the end of a list  extend: add all elements from an iterable to the end of

the list

 count: count the number of occurrences of a value  pop: remove an element from the end of a list  sort: sort the elements of a list

These methods (except count) mutate the list Compare to sorted(x), which returns a new list Call dir(list) to see a full list of attributes

List Methods

slide-77
SLIDE 77

List Comprehensions

slide-78
SLIDE 78

We can construct a list using a list comprehension, which is similar to a generator expression

List Comprehensions

slide-79
SLIDE 79

We can construct a list using a list comprehension, which is similar to a generator expression

List Comprehensions

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

slide-80
SLIDE 80

We can construct a list using a list comprehension, which is similar to a generator expression

List Comprehensions

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

>>> [3 / x for x in range(4) if x != 0] [3.0, 1.5, 1.0]

slide-81
SLIDE 81

We can construct a list using a list comprehension, which is similar to a generator expression

List Comprehensions

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

  • Evaluates to a list.

>>> [3 / x for x in range(4) if x != 0] [3.0, 1.5, 1.0]

slide-82
SLIDE 82

We can construct a list using a list comprehension, which is similar to a generator expression

List Comprehensions

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

  • Evaluates to a list.
  • <iter exp> is evaluated once.

>>> [3 / x for x in range(4) if x != 0] [3.0, 1.5, 1.0]

slide-83
SLIDE 83

We can construct a list using a list comprehension, which is similar to a generator expression

List Comprehensions

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

  • Evaluates to a list.
  • <iter exp> is evaluated once.
  • <name> is bound to an element, and <filter exp> is
  • evaluated. If it evaluates to a true value, then <map exp>

is evaluated, and its value is added to the resulting list. >>> [3 / x for x in range(4) if x != 0] [3.0, 1.5, 1.0]

slide-84
SLIDE 84

  

Dictionaries

slide-85
SLIDE 85

Sequences map integers to values

 

Dictionaries

slide-86
SLIDE 86

Sequences map integers to values

 

Dictionaries

>>> a = [3, 1, 2]

slide-87
SLIDE 87

Sequences map integers to values

 

Dictionaries

>>> a = [3, 1, 2] ‐3 ‐> 3 0 ‐> 3 ‐2 ‐> 1 1 ‐> 1 ‐1 ‐> 2 2 ‐> 2

slide-88
SLIDE 88

Sequences map integers to values What if we wanted arbitrary values in the domain?

Dictionaries

>>> a = [3, 1, 2] ‐3 ‐> 3 0 ‐> 3 ‐2 ‐> 1 1 ‐> 1 ‐1 ‐> 2 2 ‐> 2

slide-89
SLIDE 89

Sequences map integers to values What if we wanted arbitrary values in the domain?

Dictionaries

>>> a = [3, 1, 2] 'cain' ‐> 2.79 'bumgarner' ‐> 3.37 'vogelsong' ‐> 3.37 'lincecum' ‐> 5.18 'zito' ‐> 4.15 ‐3 ‐> 3 0 ‐> 3 ‐2 ‐> 1 1 ‐> 1 ‐1 ‐> 2 2 ‐> 2

slide-90
SLIDE 90

Sequences map integers to values What if we wanted arbitrary values in the domain? We use a dictionary

Dictionaries

>>> a = [3, 1, 2] 'cain' ‐> 2.79 'bumgarner' ‐> 3.37 'vogelsong' ‐> 3.37 'lincecum' ‐> 5.18 'zito' ‐> 4.15 ‐3 ‐> 3 0 ‐> 3 ‐2 ‐> 1 1 ‐> 1 ‐1 ‐> 2 2 ‐> 2

slide-91
SLIDE 91

Sequences map integers to values What if we wanted arbitrary values in the domain? We use a dictionary

Dictionaries

>>> a = [3, 1, 2] 'cain' ‐> 2.79 'bumgarner' ‐> 3.37 'vogelsong' ‐> 3.37 'lincecum' ‐> 5.18 'zito' ‐> 4.15 >>> eras = {'cain': 2.79, 'bumgarner': 3.37, 'vogelsong': 3.37, 'lincecum': 5.18, 'zito': 4.15} >>> eras['cain'] 2.79 ‐3 ‐> 3 0 ‐> 3 ‐2 ‐> 1 1 ‐> 1 ‐1 ‐> 2 2 ‐> 2

slide-92
SLIDE 92

 

Dictionary Features

slide-93
SLIDE 93

Dictionaries are not sequences, but they do have a length and are iterable

 

Dictionary Features

slide-94
SLIDE 94

Dictionaries are not sequences, but they do have a length and are iterable

 Iterating provides each of the keys in some arbitrary order

 

Dictionary Features

slide-95
SLIDE 95

Dictionaries are not sequences, but they do have a length and are iterable

 Iterating provides each of the keys in some arbitrary order

 

Dictionary Features

>>> total_era = 0 >>> for pitcher in eras: ... total_era += eras[pitcher] ... >>> total_era / len(eras) 3.772

slide-96
SLIDE 96

Dictionaries are not sequences, but they do have a length and are iterable

 Iterating provides each of the keys in some arbitrary order

Dictionaries are mutable

Dictionary Features

>>> total_era = 0 >>> for pitcher in eras: ... total_era += eras[pitcher] ... >>> total_era / len(eras) 3.772

slide-97
SLIDE 97

Dictionaries are not sequences, but they do have a length and are iterable

 Iterating provides each of the keys in some arbitrary order

Dictionaries are mutable

Dictionary Features

>>> total_era = 0 >>> for pitcher in eras: ... total_era += eras[pitcher] ... >>> total_era / len(eras) 3.772

>>> eras['lincecum'] = 3.0

slide-98
SLIDE 98

Dictionaries are not sequences, but they do have a length and are iterable

 Iterating provides each of the keys in some arbitrary order

Dictionaries are mutable There are dictionary comprehensions, which are similar to list comprehensions

Dictionary Features

>>> total_era = 0 >>> for pitcher in eras: ... total_era += eras[pitcher] ... >>> total_era / len(eras) 3.772

>>> eras['lincecum'] = 3.0

slide-99
SLIDE 99

Dictionaries are not sequences, but they do have a length and are iterable

 Iterating provides each of the keys in some arbitrary order

Dictionaries are mutable There are dictionary comprehensions, which are similar to list comprehensions

Dictionary Features

>>> total_era = 0 >>> for pitcher in eras: ... total_era += eras[pitcher] ... >>> total_era / len(eras) 3.772

>>> eras['lincecum'] = 3.0

>>> {p: round(eras[p]‐1, 3) for p in eras} {'zito': 3.15, 'cain': 1.79, 'bumgarner': 2.37, 'lincecum': 2.0, 'vogelsong': 2.37}

slide-100
SLIDE 100

 

 

 

Limitations on Dictionaries

slide-101
SLIDE 101

Dictionaries are unordered collections of key‐value pairs.

 

 

Limitations on Dictionaries

slide-102
SLIDE 102

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

 

 

Limitations on Dictionaries

slide-103
SLIDE 103

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

 A key of a dictionary cannot be an object of a mutable

built‐in type.

 

Limitations on Dictionaries

slide-104
SLIDE 104

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

 A key of a dictionary cannot be an object of a mutable

built‐in type.

 Two keys cannot be equal. There can be at most one value

for a given key.

 

Limitations on Dictionaries

slide-105
SLIDE 105

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

 A key of a dictionary cannot be an object of a mutable

built‐in 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.

Limitations on Dictionaries

slide-106
SLIDE 106

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

 A key of a dictionary cannot be an object of a mutable

built‐in 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 an intentional consequence of the dictionary abstraction.

Limitations on Dictionaries