Dictionary
Module 1 — What is a dict?
A memory that stores values over a Key reinvented.
Definition
A Dictionary (short dict) is a collection of
Key value pairs. Instead of using an index like with lists, you access a value via its Key to — usually a string.
Syntax:
name_des_dicts = {
"schluessel1": wert1,
"schluessel2": wert2,
}
An empty dictionary is created {}. In modern Python (from 3.7) the order remains the key obtained — it corresponds to the order when creating or adding.
Note on exercise: In the first four parts you set the dictionary values
even in the code fixed - the focus is on creating, reading, replacing and adding to it. In part 5 and 6 you read the values input() on (from the module
Variables known); There the check automatically feeds in fixed inputs and compares your output character by character.
Example 1.1 — create and output a dictionary
Loading Python…Tests
Module 2 — Reading and changing values
Key access — like a label on a drawer.
Read a value
With d["schluessel"] you get the saved value back. If the key doesn't exist, there is one KeyError.
Example 2.1 — read out individual values
Loading Python…Tests
Overwrite an existing value
If you write to an existing key, the old value becomes
replaced. The syntax is exactly the same as reading — just on the left side of one =.
Example 2.2 — Change value
Loading Python…Tests
Module 3 — New keys and conditions
Dictionaries simply grow with you.
Create a new key
You write on one not yet existing key, it is added. This is the same command as overwrite.
Example 3.1 — Add key
Loading Python…Tests
Values with if check
You can use a dictionary value like any other variable in a condition:
if d["preis"] > 10:. This allows small rules to be formulated based on stored values.
This one is enough here if-Query as Pre-taste — conditions with
if, elif and else you'll learn in the next module (if, elif, else) know in detail.
Example 3.2 — Check value with if
Loading Python…Tests
Module 4 — Calculating with values
The stored values are normal numbers or strings.
Example: Average of two grades
If the values are numbers, you can calculate with them as usual. Just get involved d["schluessel"] to and use the result in an expression.
durchschnitt = (d["note1"] + d["note2"]) / 2
Example 4.1 — Average of Dictionary Values
Loading Python…Tests
TipKeys are strings
Write keys in quotation marks: d["alter"], not d[alter]. Without quotes, Python would search for a Variables search with this name - and this almost always throws an error.
Sources & further literature
Where the knowledge imparted here is officially documented.
Official Python documentation
- Python tutorial — 5.5. Dictionaries: docs.python.org/3/tutorial/datastructures.html#dictionaries
- Python Standard Library — Mapping Types — dict: docs.python.org/3/library/stdtypes.html#mapping-types-dict
Tutorials & Manuals
- Real Python — Dictionaries in Python: realpython.com/python-dicts
- Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023 — Chapter on dictionaries.
- Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021 — “Dictionaries” section.