Python Dictionary Flashcards

 

card 1 of 11

 

type({ 0:'abc', 1:True, 2:[0.5,0.25] })

 

curly braces denote a dictionary

<type 'dict'>

card 2 of 11

 

type({})

 

curly braces denote a dictionary

<type 'dict'>

card 3 of 11

 

{ 0:True, 1:99, 0:80, 2:'abc' }

 

there should be only one value per key

{0:80, 1:99, 2:'abc'}

card 4 of 11

 

len({1:2,2:3,3:4,4:5,3:6})

 

len() function counts number of keys

4

card 5 of 11

 

len({})

 

len() function counts number of keys

0

card 6 of 11

 

{'a':True, 'b':False, 3:700}[0]

 

indexing dictionary is by key

KeyError: 0 is not a key in the dictionary

card 7 of 11

 

{'a':True, 'b':False, 3:700}['b']

 

indexing dictionary is by key

False

card 8 of 11

 

12 in {'a':True, 1:12, 0:'a'}

 

the "in" operator tests for key in dictionary

False

card 9 of 11

 

{True:[1,2,3],False:'abc'}[True][2]

 

evaluate indexing left-to-right

3

card 10 of 11

 

Could a dictionary contain within it, a dictionary?

 

values associated with keys can be any Python type

Yes, for example, "{0:{'a':True}}"

card 11 of 11

 

{{0:1}:'a',True:'b'}[{0:1}]

 

this one may seem illogical

TypeError - dictionary keys should be numbers, strings, or booleans (later we may see exceptions to this, but not many)

 

End of Problem Set

(use browser reload to restart this problem set)