Today I was very bored so I checked my bonus assignments and I got this.

  • Write a Python program that adds a new key-value pair to a dictionary only if the key doesn’t exist already.

  • If the key-value pair exists in the dictionary, do not update the existing value. The dictionary should not be modified in this case.

  • Store the new key in the new_key variable and the new value in the new_value variable. Print the final value of the dictionary.

oh btw, I’m not going to explain this one…. although I’ll make steps…

Steps:

  1. Make dictionary
  2. Make new variables
  3. use if statement and append
  4. print dictionary.

Ta da:


dict = {"Jan": 1, "Feb": 2, "Mar": 3}
 
new_key = "Feb"
new_value = 4
 
if new_key not in dict:
    dict[new_key] = new_value
 
print(dict)

Thank you.