A school supply room tracks stock with the Python code below. Trace the code, then fill in the table with the final result of each expression. Enter integers for numbers and True or False for the membership test.
stock = {"pens": 12, "pads": 5, "tape": 7} stock["pens"] -= 4 stock["clips"] = 30 stock["pads"] = stock.get("pads", 0) + stock.get("glue", 3) removed = stock.pop("tape") stock["clips"] += removed
Unlock full course notes by signing up
Tracing update, a loop and a comprehension on stock levels
A greengrocer tracks how many crates of each fruit are in the storeroom. The Python code below merges a delivery record into the stock, tops up low items, removes some apples for a display, and then builds a dictionary of items that still need ordering.
After the code runs, give each of the following as a whole number: the number of keys in stock, the value stored for "pears" in stock, the total of all values in stock, and the number of keys in low.
Lookups, iteration and membership in dictionaries
A cafe stores its fruit prices in dollars as prices = {"apple": 0.5, "pear": 0.75}. Consider each statement below about this dictionary on its own, starting from this original value. Select every statement that is true.
Predicting the iteration order of a dictionary
In Python 3.7 and later, a dictionary remembers the order in which its keys were inserted. Consider this program, which records signal colours and then prints the keys of the dictionary.
Arrange the keys in the order the loop prints them, from first printed to last printed.