runtric-logo
Python Basic 2 - DataDictionaries: key-value pairs (3/5)
Chapter 3
Python Basic 2 - Data

Dictionaries: key-value pairs

6 minutes read

Beginner friendly

Includes practice

Ever used a real-world dictionary or a phone contact list?
You look up a word (or a name) and instantly get its meaning (or number).
Python’s dictionary works the same way: one side is the key, the other the value.

Runtic's Learning Tips

  • Struggling at first is normal! Every programmer once typed KeyError for the first time.

  • Focus on why a step works, not just what to type; your brain forms stronger links that way.

  • Small daily practice beats a weekend marathon. Ten minutes of “dict” tinkering today builds tomorrow’s confidence.

What is a Dictionary?

A dictionary (dict) is an unordered collection that stores data as pairs:

key ➔ value

Example (a tiny fruit shop stock):

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

  • Keys must be unique and immutable (strings, numbers, tuples).
  • Values can be anything (numbers, lists, even other dictionaries).

Creating Your First Dictionary

Inline (literal) method:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Or start empty then add:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Try it yourself below.

Practice Challenge

Interactive Code Editor
📝 Your Task:

Create a dictionary named book_priceswith three titles as keys and their prices as values. Then, store the number of items in the dictionary inside len_resultusing the len()function, and print it to confirm.

python

Create a dictionary with exactly three key–value pairs, then use len(...) to get its size.

Accessing Values by Key

Get a value using square brackets:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Safer: dict.get(key, default) avoids crashes if the key is missing.

Adding or Updating Entries

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Removing Entries

del stock["orange"] removes that pair.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

stock.pop("apple", 0) removes and returns the value (0 is fallback).

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Does This Key Exist?

"pear" in stockTrue or False

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Useful to prevent errors before accessing.

Looping Through a Dictionary

To go through every key–value pair in a dictionary, use a for loop with two variables: one for the key and one for the value.
This lets you access both parts of each entry inside the loop.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

  • .keys() – iterate keys only.
  • .values() – iterate values only.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Quick Check-in

Quick Practice

3 minutes

Choose the best answer for each mini-quiz!

Which statement is correct?

`stock.get('mango', 0)` returns ___ if 'mango' is not present.

How would you remove key 'Alice' from dict `grades`?

Practice: Retrieve a Value

Practice Challenge

Interactive Code Editor
📝 Your Task:

Use the given dictionary stockto get the number of bananas and store it in banana_qty.Then, print the result to verify.

python

Access the value of "banana" using square brackets or .get().

Quick Summary

  • Dictionaries map unique, immutable keys to values of any type.

  • Common actions: create, access, add/update, delete, test membership, iterate.

  • .get() and the in keyword keep your code safe from missing-key errors.

Lesson Complete!
100%
Progress
What You've Learned:
  • You built, queried, and modified Python dictionaries with confidence.
  • You practiced safe key access and removal to avoid runtime errors.
  • You linked everyday analogies (phonebook) to solid code understanding.

Sign in to track your progress

Chapter Progress

0 secs

AI Tutor

Powered by Runtric

Solve first — tutor's here to guide.

Runtric might be wrong. Think critically.