Jul-28-2020, 03:58 PM
Use Colors['Sam'] to get Sam's favorite color. You can also use Colors.get('Sam'). get is useful when looking up keys that may not be in the dictionary. Colors['Bob'] will throw an exception, but Colors.get('Bob', 'Green') will return 'Green', a default value used when the key is not found.
This code can use [] because the keys are guaranteed to be in the dictionary.
This code can use [] because the keys are guaranteed to be in the dictionary.
Colors = {'Sam': 'Blauw', 'Ali': 'Rood', 'Saar': 'Geel'} for Item in Colors: # print("{0} houdt van de kleur {1}.".format(Item, Colors[Item])) # Works print(f'{Item} houdt van de kleur {Colors[Item]}.')) # Works, shorter, reads better