Dictionary adalah seperangkat nilai (set of value) yang tidak beraturan dalam sepasang kurung: { }
Angka integer (angka 1, 2, 3....) and string (kata, halo, apa kabar dsb) bisa dipakai dalam index dictionary.
Dictionary sederhana:
words = { }
words["Hello"] = "Bonjour"
words["Yes"] = "Oui"
words["No"] = "Non"
words["Bye"] = "Au Revoir"
print(words["Hello"])
Output = Bonjour
print(words["No"])
Output = Non
Limited to single word definitions
dict = { }
dict["Ford"] = "Car"
dict["Python"] = "The Python Programming Language"
dict[2] = "This sentence is stored here"
print(dict["Ford"])
Output : Car
print(dict["Python"])
Output: The Python Programming Language
print(dict["2"])
Output: The Python Programming Language ....> salah
Note: code ["2"]), diganti menjadi [2])
print(dict[2])
Output: This sentence is stored here
Manipulating the Dictionary
words = {}
words["Hello"] = "Bonjou"
words["Yes"] = "Oui"
words["No"] = "Non"
words["Bye"] = "Au Revoir"
print(words)
Output: {'Hello': 'Bonjou', 'Yes': 'Oui', 'No': 'Non', 'Bye': 'Au Revoir'}
del words["yes"]
print(words)
words["yes"] = "Oui"
print(words)
# Previous postings:
JJJJ
No comments:
Post a Comment