Key-Value-Daten mit Dict
Suchen Sie Werte anhand eines Schlüssels nach.
Key-Value-Daten mit Dict ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Look Up by Key
When you want to find a value by a name instead of a position, use a Dict. It maps keys to values. 🔑
Two Types in Brackets
A Dict declares two types: the key type and the value type, like Dict[String, Int].
var ages = Dict[String, Int]()Add a Pair
Store a value by assigning through its key in brackets. This inserts the key-value pair into the dict.
ages["Ada"] = 36Read a Value
Fetch a value by putting its key in brackets. The dict returns whatever you stored under that key.
var a = ages["Ada"]Update in Place
Assigning to an existing key overwrites its value. Keys stay unique, so you never get duplicates.
ages["Ada"] = 37Check Membership
Test whether a key exists with the in operator. It returns True or False without raising an error.
if "Ada" in ages:
print("found")Size of a Dict
Count the pairs with len. It reports how many key-value entries the dict currently holds.
var count = len(ages)Iterate the Keys
Looping a dict walks its keys. Use each key to reach its value inside the loop body.
for name in ages:
print(name)Remove a Pair
Delete an entry with the pop method, passing the key. It removes the pair and returns its value.
var removed = ages.pop("Ada")Keys Must Be Hashable
A dict finds values fast because keys are hashable. Strings and Ints work great as keys.
Fast Average Lookup
Lookups, inserts, and updates are roughly constant time. That speed is why dicts power so much real code.
Quick Check
A Dict already has the key "Ada". You assign ages["Ada"] = 37. What happens to the dict size?
Recap
A Dict maps unique keys to values for fast lookup. Use brackets to set and get, in to check, and len to count. 🎯
Häufig gestellte Fragen
Ist die Lektion „Key-Value-Daten mit Dict“ kostenlos?
Ja — der vollständige Text von „Key-Value-Daten mit Dict“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Key-Value-Daten mit Dict“?
Suchen Sie Werte anhand eines Schlüssels nach. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Key-Value-Daten mit Dict“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Eine Liste erstellen und erweitern
- Key-Value-Daten mit Dict
- Eindeutige Elemente mit Set
- Die richtige Collection wählen