Using Python 3+ Given the following dictionary: myDictionary = {\"Red\": 102, \"
ID: 3705750 • Letter: U
Question
Using Python 3+ Given the following dictionary:
myDictionary = {"Red": 102, "Green": 612, "Blue": 920} #use this as your first line.
Print the answer(s) you find for each of the following, but do not keep re-creating the dictionary each time with the initial values. The myDictionary should keep changing.
Find the length of the Dictionary called: myDictionary. Print the length.
In myDictionary, use the get( ) function to retrieve the value of the key that is "Blue", print the value. Print the value.
Find the sum of the "values" in the myDictionary. Print the sum. Use a for loop:
total = 0
for key in myDictionary:
#then pull out the “value” based on the key and add it to the total.
Answer True or False – based on an "if" statement - if the value for the key of "Red" is 612.
Change the "value" for the key that equals "Green" to be 710. Print the myDictionary.
Add the following Key-Value pair to myDictionary: Key = "Orange", Value = 832. Then print the myDictionary.
Replace the value in "Red" to be 523. Print myDictionary.
Add into myDictionary (append), a dictionary called yourDictionary = {"Yellow": 1040, "Orange": 1214}. Print the new myDictionary, with the appended values.
Delete the following "key-value" pair ("Yellow":1040). Print the myDictionary.
Check the code and make sure it is working correctly.
Explanation / Answer
if __name__ == '__main__': myDictionary = {"Red": 102, "Green": 612, "Blue": 920} print(len(myDictionary)) print(myDictionary.get("Blue")) total = 0 for key in myDictionary: total += myDictionary.get(key) print(total) if myDictionary.get("Red") == 612: myDictionary["Green"] = 710 print(myDictionary) myDictionary["Orange"] = 832 print(myDictionary) myDictionary["Red"] = 532 print(myDictionary) yourDictionary = {"Yellow": 1040, "Orange": 1214} myDictionary.update(yourDictionary) print(myDictionary) myDictionary.pop("Yellow") print(myDictionary)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.