0% found this document useful (0 votes)
22 views10 pages

Python Dictionaries

python code tutorials

Uploaded by

Farhan Bajwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views10 pages

Python Dictionaries

python code tutorials

Uploaded by

Farhan Bajwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Exploring Python

Dictionaries
An interactive journey for young coders

Age 12+ Coding Workshop


10 August 2025
What is a Dictionary?

A dictionary stores information using key–value pairs.


Keys must be unique and hashable (like strings, numbers, tuples).
Values can be any data type.
Order: since Python 3.7, items remain in the order they were added.
Dictionaries are mutable and efficient hash tables that can grow and shrink
on demand.

[1][2]
Real-Life Analogies

Real dictionary → word & definition


name: Alex

Lockers → locker number & contents

age: 12
Contact list → name & phone

city: Bangkok

[7]
Creating Dictionaries

student = {

"name": "Alex",

"age": 12,

Use curly braces {} with colon-separated key–value pairs "favorite_sport": "soccer"


Keys can be strings, numbers or tuples; values can be any type }
You can create an empty dictionary with {} or dict() print(student)
Use dict() to build from a list of pairs or keyword arguments

Tip: Use dict() when you want to build a dictionary


dynamically.

[3]
Accessing & Modifying Values

student = {"name": "Alex", "age": 12, "favorite_sport":


"soccer"}
Access a value using student[key] or [Link](key) print(student["name"]) # access
Add or update items by assignment: student["city"] = "Bangkok" student
Remove items: del student[key], [Link](key), [Link](), ["age"] = 13 # update
[Link]() student
["city"] = "Bangkok" # add
del student["favorite_sport"] # remove
print(student)

Add/
Access Remove
Update

del student[key] – removes item by key


[Link](key) – removes item and returns value
[Link]() – removes last inserted pair
[Link]() – empties dictionary
[4][5]
Characteristics & Restrictions

Lists Dictionaries

Access By position By key


(index)
Mutable: values can be changed in place
Dynamic: can grow or shrink as needed
Efficient hashing provides quick lookups Order Position Insertion order
matters (3.7+)
Ordered: maintains insertion order (Python 3.7+)
Keys must be hashable & unique
Values can be any type (including another dictionary) Duplicate keys? Allows Not allowed
duplicates

Key type Not applicable Must be


hashable

[2][4]
Looping Through Dictionaries

student = {"name": "Alex", "age": 12, "city": "Bangkok"}


for key in [Link]():

print(key) Use keys() to iterate over keys


Use values() to iterate over values
for value in [Link](): Use items() to iterate over key–value pairs
Dictionary comprehensions build dictionaries in one line
print(value)

for key, value in [Link]():

print(f"{key}: {value}")

Iterate easily!
[6]
Hands-on Activity 1: Contact Book

contacts = {

"Alice": "0812345678",

Work in pairs to create a contact book. "Bob": "0898765432",


1. Start with the dictionary below
2. Print the contacts "Cara": "0852468013"
3. Add a new friend }
4. Update one phone number
5. Remove a contact
6. Print the updated book and share your results!
Team up and have fun!

[4][5]
Hands-on Activity 2: Shopping Cart

cart = {"Pencils": 15, "Notebooks": 40, "Backpack": 350}


total
Create a shopping cart dictionary and calculate the total. =0
1. Use the template on the right to create your cart for item, price in [Link]():
2. Loop through items and prices
3. Sum the prices to get the total print(item, price)
4. Print each item with its price and the total cost total
5. Experiment: add more items or calculate the average price! += price
print("Total:", total)

Calculate & compare!


[6]
Mini Project & Group Discussion

Mini Project: Build a Student Gradebook Group Discussion


• Create a dictionary where each key is a student's name and the • Where might you use dictionaries in real life? (e.g.,
value is another dictionary of subjects and grades. translation, contact lists, inventories)
• Write code to calculate each student's average grade. • How do dictionaries differ from lists and why is that
• Allow adding new subjects or students. useful?
• Print the gradebook neatly. • Share your mini project ideas with the group!

Create something amazing! Share & reflect together!

[2]

You might also like