Reading about Python? Actually practice it. Try PyChallenge free

Python Tutorial

Dictionary

Ein Dictionary (engl: Wörterbuch) kann als eine Gruppe von Schlüssel - Werte paare gesehen werden.

Jeder Schlüssel hat einen Wert. Ein Paar von Klammern macht ein leeres Dictionary: {}

Practice Python with interactive exercises

Beispiel Zum bespiel:

#!/usr/bin/python
 
words = {}
words["Hello"] = "Bonjour"
words["Yes"] = "Oui"
words["No"] = "Non"
words["Bye"] = "Au Revoir"
 
print(words["Hello"])
print(words["No"])

Erstens wird den dictionairy definiert. In diesem Beispiel ist "Hello" Schlüssel und "Bonjour" ein wert. Am Ende Printn wir individuellen werten via Schlüssels.

Es gibt kein Wort Limit:

#!/usr/bin/python
 
dict = {}
dict['Ford'] = "Car"
dict['Python'] = "The Python Programming Language"
dict[2] = "This sentence is stored here."
 
print(dict['Ford'])
print(dict['Python'])
print(dict[2])

Manipulieren von einem dictionary

#!/usr/bin/python
 
words = {}
words["Hello"] = "Bonjour"
words["Yes"] = "Oui"
words["No"] = "Non"
words["Bye"] = "Au Revoir"
 
print(words)            # print key-pairs.
del words["Yes"]       # delete a key-pair.
print(words)            # print key-pairs.
words["Yes"] = "Oui!"  # add new key-pair.
print(words)            # print key-pairs.
BackNext
Practice
Stop reading. Start writing Python.
PyChallenge gives you interactive exercises in your browser — no install needed.
Practice Python with interactive exercises