First page Back Continue Last page Overview Graphics

Example

>>> prices = {'milk': 1.29, 'butter': 1.79, 'egg': 1.99}

>>> print(prices)

{'milk': 1.29, 'butter': 1.79, 'egg': 1.99}

>>> prices["milk"]

1.29

>>> prices["bananas"] = 2.99

>>> print(prices)

{'milk': 1.29, 'bananas': 2.99, 'butter': 1.79, 'egg': 1.99}

>>> prices["apples"]

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

KeyError: 'apples'

>>> prices.get("apples")

>>> prices.get("apples", 3.49)

3.49

>>> prices.get("bananas", 3.49)

2.99

>>>