0:37 - First example (Student using a dictionary)
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'ComSci']}
print(student['name'])
'''
out:
John
'''
1:32 – [ ] Square bracket access of the dict
2:04 – Dict items can be many things, they’re not bound to one “type”
2:14 – Keys can be any immutable data type
2:44 – Accessing a key that does not exist
3:00 – Alternative to “throwing an error” if a key does not exist
3:09 - .get( ) access of the dict
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'ComSci']}
print(student.get('name'))
'''
out:
John
'''
### accessing a key that does not exist with the .get method as opposed to [ ] square bracket access
print(student.get('phone'))
'''
out:
None
'''
### accessing a key that does not exist with the .get method with defalut value
print(student.get('phone', 'Not Found'))
'''
out:
Not Found
'''
### accessing a key that exist with the .get method with defalut value
student['phone'] = '555-5555'
print(student.get('phone', 'Not Found'))
'''
out:
555-555
'''
5:02 – .update() method
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'ComSci']}
student.update({'name': 'Jane', 'age': 26, 'phone': '555-5555'})
print(student)
'''
out:
{'name': 'Jane', 'age': 26, 'courses': ['Math', 'ComSci'], 'phone': '555-5555'}
'''
5:57 – Deleting a specific key and its value
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'ComSci']}
# del way
del student['age']
print(student)
'''
out:
{'name': 'John', 'courses': ['Math', 'ComSci']}
'''
# pop way
# Remember the .pop() method not only removes the item put pops it off or returns it to you
age = student.pop('age')
print(student)
print(age)
'''
out:
{'name': 'John', 'courses': ['Math', 'ComSci']}
25
'''
7:13 – Finding out the number of keys in dict with len() function
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'ComSci']}
print(len(student))
'''
out:
3
'''