3) d. Dictionary Examples (2)




NASA is curious about the possibility of being attacked by alien species. You were asked to count the population of every known colony all around the Universe.
Create a program that tracks the population of the different species using dictionaries, where the key is the planet’s name and the value is the population of that planet.

1
2
Output:
{'Earth': 8965845324, 'Jupyter': 30051654654, 'Saturn': 15654431, 'Exo Planet 3': 15546648, 'Haumea': 1564456}




You are driving on a highway and your car’s brakes are not working anymore. You have all access to its onboard computer. You found out that somebody hacked your car and removed the braking system from your car’s software. Append a new element into the car’s system specification with your braking system and turn it on to be able to stop. Name the system “brake” and set the value to True.

1
2
3
Output:
Before update:  {'Steering': True, 'Fueling': True, 'Radio': True, 'GPS': True, 'Lights': True}
After update:  {'Steering': True, 'Fueling': True, 'Radio': True, 'GPS': True, 'Lights': True, 'Brake': True}




You just cheated on your girlfriend, but you want to hide it from her. You just found out, that if 5 minutes she will be at your place to check your messages.
Write a program where you change the message written to “Jessica” to “Hy” in order to hide your mistake. If you already deleted the message do nothing.

1
2
3
Output:
Messages before changing them:  {'Jessica': 'I want to meet you today night at your place. What do you think?', 'Erik': "Hy bro what's up", 'Carol': 'Can you help me with mats?'}
Messages after changing them: {'Jessica': 'Hy', 'Erik': "Hy bro what's up", 'Carol': 'Can you help me with mats?'}




Domenico also has some clients that married couples. He wants one entry in the dictionary and stories to use a list as a dictionary key as follows:
>>> ddict[[‘Alice’, ‘Bob’]] = 33 Is that a valid statement? Why?

1
2
Output:
{'Alice': 1, 'Bob ': 2, 33: ['Alice', 'Bob']}