Class 30 — April 2
It’s a fact, look it up
Really need to know — What lies farther down the road — Need a future map
Look both ways
Agenda
- Consider
dict
- hacer 뭔가 frais oggi
Nuances
Maxims (among others)
- Read the problem in its entirety before problem solving
- Determine for yourself why the sample problem solutions are as they are
- Problem solve before you code
- Check whether your solution produces the same answers as the sample runs
- Check whether your solution matches problem specifications
Problems
- Problem good_doggie.py
- Problem what_did_you_say.py
Huh?
- dónde está mi lápiz
- donde esta mi lapiz
- hvor 是 该 tumili maleri av Munch
- est la baguette fraîche aujourd'hui
- est la baguette fraiche aujourd'hui
Discussion
- The human brain is great with associations; e.g., we typically associate apple with red and January 1st with New Year's Day.
- A
dict
is Python's software equivallent of a collection of associations.
- In particular, a
dict
is unnordered collection of associations.
- The elements of
dict
are called mappings. A mapping associates one object (called the key) with another object (called the value).
- The literal for an empty
dict
is{}
.
- A literal mapping is written using the
:
to separate the key with its value. For example, the variableflora_to_color
defined below is adict
that associates flora with colors.
flora_to_color = { 'spruce': 'green', 'apple': 'red', 'shamrock': 'green',
'banana': 'yellow', 'potato': 'brown', 'eggplant': 'purple' }
- The
len()
function works on adict
.
len(
flora_to_color
)
is 6 because the definition offlora_to_color
included six mappings.
- The brackets
[]
operator in conjunction with assignment can be used to put a mapping into adict
.
- The statement below adds the mapping
'lemon'
to'yellow'
.
flora_to_color[ 'lemon' ] = 'yellow'
- The keys of a
dict
are always distinct. If there had already been a mapping with'lemon'
as the key, the above assignment would replace the association.
- As our
flora_to_color
example shows, the values of adict
are not necessarily unique.
- Other information about
dict
objects are listed below. In the below, assume thatd
is adict
.
d.get(k)
returns the value that keyk
maps to ind
. If there is no such value, returnsNone
.
del d[k]
removesk
’s mapping ind
.
(k in d)
indicates whetherk
is a key value ind
.
(k not in d)
indicates whetherk
is not a key value ind
.
d.clear()
removes all mappings ind
.
d.keys()
are the keys ford
. There are no duplicates among the keys.
d.values()
are the values of the keys ind
. There are duplicate values if more than one key maps to the same value.
- Information on a
dict
is always available in the module documentation section of the website.
- Additional information on collections is in general is available in the collections epistile.
To do
- Look over artifacts
Warning: fopen(/l/cs1112/public_html/defs/htm/181-trailer): Failed to open stream: No such file or directory in /l/cs1112/public_html/defs/php/read.php on line 5
Unable to open file!