= {'colour' : 'verte', 'point' : 5}
d_0
= d_0['point'] #square brackets to access values
access print(f"You have earned {access} points!")
You have earned 5 points!
Python basics
Kunal Khurana
October 6, 2023
A dictionary in Python is a collection of key-value pairs. 1. looping through a dictionary 2. work with information stored in a dictionary 3. access and modify individual elements in a dictionary 4. nest multiple dictionaries in a list, nest lists in a dictionary, and nest a dictionary inside a dictionary
The new colour is green.
user_0 = {'dob' : 'nov 1995',
'birth_place' : 'gugaron',
'education' : 'masters',
'children' : '2',
}
for key, value in user_0.items():
print(f"\nKey: {key}")
print(f"Value: {value}")
Key: dob
Value: nov 1995
Key: birth_place
Value: gugaron
Key: education
Value: masters
Key: children
Value: 2
Kunal
Ritika
Kartik
Vaibhav
{'dob': 'nov 1995', 'birth_place': 'gugaron', 'education': 'masters', 'children': '2'}
{'tout va bien': 0, 'oui, ça va': 1}
{'kunal': 'français', 'ritika': 'espagneol', 'kartik': 'russe', 'vaibhav': 'almande', 'vaisahli': 'français'}
programming_languages = {'kunal' : ['python', 'latex', 'html', 'java'],
'john' : ['html', 'c', 'C++'],
'gofi' : ['java', 'python', 'R', 'html']}
for name, languages in programming_languages.items(): #use items to iterate thorough dictionary items
print(f"\n{name.title()}'s favorite languages are:")
for language in languages:
print(f"\t{language.title()}")
Kunal's favorite languages are:
Python
Latex
Html
Java
John's favorite languages are:
Html
C
C++
Gofi's favorite languages are:
Java
Python
R
Html
{'kkhurana': {'first': 'kunal', 'last': 'khurana', 'location': 'montréal'},
'asharma': {'first': 'anita', 'last': 'sharma', 'location': 'sudbery'},
'jarora': {'first': 'jatin', 'last': 'arora', 'location': 'perth'}}
for username, user_info in users.items():
print(f"\nUsername: {username}")
full_name = f"{user_info['first']} {user_info['last']}"
location = user_info['location']
print(f"\tFull name: {full_name.title()}")
print(f"\tLocation: {location.title()}")
Username: kkhurana
Full name: Kunal Khurana
Location: Montréal
Username: asharma
Full name: Anita Sharma
Location: Sudbery
Username: jarora
Full name: Jatin Arora
Location: Perth