Change Values
Lesson 5 of 14 in Coddy's Dictionary in Python course.
To change the value of a specific item refer to its key name,
my_dict = {"name": "Alice", "age": 30, "city": "New York"}
my_dict["age"] = 31 # Update the age
It's also possible to use the update() method,
my_dict.update({"age": 31})Both of the operations does the same
Challenge
EasyYou are given a stock dictionary, your task:
- Update the stock of apples to 12.
- Reduce the stock of cherries by 2.
- Increase the stock of dates by 3.
- Decrease the stock of bananas by 5.
Try it yourself
# The stock dictionary
stock = {
"apple": 10,
"banana": 15,
"cherry": 5,
"date": 8
}
# Your code here:
# Don't change below this line
print("Updated Stock:", stock)