DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1
>>> type(cabinet)
<class 'dict'>

Instead of using an index, you use the key, as shown in
this example:


>>> cabinet["scores"]
(98, 76, 95)
>>> cabinet["company"]
'Cisco'

To add more items to a dictionary, you can assign them
with a new key. You can even add another dictionary to
your existing dictionary, as shown in this example:


Click here to view code image


>>> cabinet["address"] = {"street":"123 Anywhere
Dr",
"city":"Franklin", "state":"TN"}
>>> cabinet["address"]
{'street': '123 Anywhere Dr', 'city': 'Franklin',
'state': 'TN'}

Sets


A set in Python consists of an unordered grouping of
data and is defined by using the curly braces of a
dictionary, without the key:value pairs. Sets are mutable,
and you can add and remove items from the set. You can
create a special case of sets called a frozen set that makes
the set immutable. A frozen set is often used as the
source of keys in a dictionary (which have to be
immutable); it basically creates a template for the
dictionary structure. If you are familiar with how sets
work in mathematics, the various operations you can
perform on mutable sets in Python will make logical
sense. To define a set, do the following:


Click here to view code image

Free download pdf