Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    PyPython

    Getting started

    • Your first Python program
    • Variables and types
    • Numbers and operators
    • Type conversion and input

    Strings

    • Strings and f-strings
    • String indexing and slicing
    • Cleaning and splitting strings

    Control flow

    • Conditions: if, elif and else
    • Loops: for and while
    • Loop control and nesting

    Functions

    • Functions with def
    • Parameters and scope

    Collections

    • Lists
    • Dictionaries
    • Tuples and sets
    • Comprehensions, enumerate and zip

    Errors, files and libraries

    • Handling exceptions
    • Modules and the standard library
    • Reading and writing files
    • Exchanging data with JSON

    Objects and lazy iteration

    • Classes and objects
    • Inheritance and composition
    • Iterators and generators

    Testing and project

    • Testing and project setup
    • Project: a grade report
    Python

    Collections · Lesson 14 of 25

    Dictionaries

    Keys and values: a small lookup table.

    7 minutes2 games

    A dictionary stores key: value pairs in { }. Read a value by its key: student["name"]. .get("x", default) returns a fallback instead of erroring when the key is missing.

    Python
    student = {"name": "Omar", "level": 3}
    student["level"] = 4
    student["city"] = "Giza"
    
    print(student["name"], student["level"])
    print(student.get("phone", "no phone"))
    
    for key, value in student.items():
        print(key, "->", value)
    ToolUse

    d[key]

    Read; KeyError if missing

    d.get(key, fallback)

    Read with a fallback

    key in d

    Test key membership

    Important

    Dictionary keys must be hashable, such as strings or numbers; a list is not a valid key.

    Interview question

    Does in search dictionary keys or values?

    Spot the bug

    One line raises a KeyError. Which?

    Exercise

    Print the price of book from the dictionary.

    Expected output: 120

    Python
    prices = {"pen": 5, "book": 120}
    

    Learn more from

    • Python documentationdocs.python.org
    • Real Python: further readingrealpython.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousListsNextTuples and sets
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.