Worked Examples · Example 10.4
Q.Example 10.4: Create dictionaries — dict1, an empty dictionary created using curly braces; dict2, an empty dictionary created using the built-in function dict(); and dict3, the dictionary that maps names of the students to respective marks in percentage.
Sikkim CbseNCERTSubjective· 2mImportance★★★★★est
27% · 8/30 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 →{} and dict() both create an empty dictionary; a dictionary literal with name: marks pairs maps each student to their percentage, accessed later by key.
The idea. A dictionary stores key-value pairs — here the student's name is the key (it identifies the record) and the marks percentage is the value. Python offers two equivalent ways to start empty: the literal {} and the constructor dict(). A populated dictionary is written as comma-separated key: value pairs inside braces.
Program (Example 10.4):
dict1 = {} # empty dictionary using curly braces
print(dict1)
dict2 = dict() # empty dictionary using the built-in function dict()
print(dict2)
dict3 = {'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
print(dict3)
print(dict3['Ram']) # access the value of a key
Verified output:
{}
{}
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
89
Key lines:
dict1 = {}anddict2 = dict()print identically as{}— same empty dictionary, two spellings. …
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.