CS计算机代考程序代写 python data structure CS1-FULL-FALL2021

CS1-FULL-FALL2021

CS1
Week 11

Dictionaries

Maíra Marques Samary

maira.

Dictionaries

Basically it represents a collection of
something.

Dictionaries
• Dictionaries are data structures in Python that can be

understood as the generalization of lists, although there is a
fundamental difference:

• With lists we use indexes (whole numbers) to refer (and
access) to the elements. With indexes we can have a
sequential relation between the elements, that is, the
element 0 is the first and the n-1 the last.

• With a dictionary, any object can act as an index; not
necessarily an integer. Then, the “indexes” in a dictionary
are not necessarily comparable, so a sequential
relationship does not make sense.

Dictionaries

• What the dictionary keeps are associations, pairs of the
type , the key being analogous to the index in
a list

• In addition, the keys are unique! They do not repeat.

Dictionaries in Python works
with a similar logic as the

normal dictionaries

H

Empty dictionary

What this code prints?

If we want we can evaluate if a key
exists in a dictionary with the word in

How to put a new key to my dictionary?

DictionaryName[key] = value

dn2[’trumpet’] = ’brass musical instrument’

How to get all the keys
from a dictionary

for

If we want to print the items
inside the dictionary