Worked Examples · Example 5.8
Q.Example 5.8:
#create a dictionary
dict1 = {'Fruit':'Apple',
'Climate':'Cold', 'Price(kg)':120}
print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold',
'Price(kg)': 120}
print(dict1['Price(kg)'])
120
dict1 = {'Fruit':'Apple',
'Climate':'Cold', 'Price(kg)':120}
print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold',
'Price(kg)': 120}
print(dict1['Price(kg)'])
120
Yanam BieapTextbookSubjective· 2mImportance★★★★★est
31% · 15/49 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →A dictionary stores key:value pairs in curly braces; print(dict1) echoes all pairs, and dict1['Price(kg)'] fetches the value 120 by its key.
The concept is the dictionary data type and key access: instead of numbering its items like a list, a dict labels each value with a unique, immutable key. You retrieve a value by asking for its key in square brackets — no searching, no positions.
#create a dictionary
>>> dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}
>>> print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}
>>> print(dict1['Price(kg)'])
120
The mapping created:
| Key | Value | Value's type |
|---|---|---|
| 'Fruit' | 'Apple' | str |
| 'Climate' | 'Cold' | str |
| 'Price(kg)' | 120 | int |
Walking through:
{'Fruit':'Apple', ...}— curly braces with colons make a dict (curly braces without colons made a set in Example 5.6 — the colon is the distinguishing mark).print(dict1)— echoes the whole dictionary. In modern Python (3.7+) dicts preserve insertion order, so the pairs appear as entered. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.