Practice by topic / Python & code / Python

Python Dictionaries Exercises

Python is the most popular language for automating work: cleaning data, renaming files, crunching numbers. Each exercise runs your real code in the browser and checks it. These 5 exercises build up step by step, starting with look up a price, update stock, safe lookup.

3 of 5 free · no sign-up needed · instant feedback

1

Look Up a Price

Free

Create <code>p</code>: the price of <code>"mouse"</code> from the <code>prices</code> dictionary.

Show the solution

p = prices["mouse"]

Right — dictionary[key] is Python's lookup, like a VLOOKUP on an exact match.

Try it in the live editor →
2

Update Stock

Free

Five laptops were sold. Reduce <code>stock["laptop"]</code> by 5, and add a new item <code>"keyboard"</code> with 12 in stock.

Show the solution

stock["laptop"] -= 5, then stock["keyboard"] = 12

Good — the same square-bracket syntax both updates existing keys and adds new ones.

Try it in the live editor →
3

Safe Lookup

Free

Create <code>rate</code>: the exchange rate for <code>currency</code> from <code>rates</code>, or <code>1.0</code> if the currency isn't in the dictionary.

Show the solution

rate = rates.get(currency, 1.0)

Exactly — .get(key, default) never crashes on a missing key, like IFERROR around a lookup.

Try it in the live editor →
4

Count by Region

Create <code>counts</code>: a dictionary of how many times each region appears in <code>orders</code> (e.g. <code>{"North": 3, ...}</code>).

Solution and instant grading: open it in the practice app (use one of your 20 free picks, or Pro).

Try it in the live editor →