Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
Kamus-kamus
A dictionary ialah sejenis data yang serupa dengan array, tetapi berfungsi dengan kunci dan nilai bukannya indeks. Setiap nilai yang disimpan dalam dictionary boleh diakses menggunakan kunci, yang boleh menjadi sebarang jenis objek (seperti string, nombor, senarai, dan lain-lain) bukannya menggunakan indeksnya untuk mengaksesnya.
Sebagai contoh, pangkalan data nombor telefon boleh disimpan menggunakan dictionary sebagaimana berikut:
phonebook = {}
phonebook["John"] = 938477566
phonebook["Jack"] = 938377264
phonebook["Jill"] = 947662781
print(phonebook)
Sebagai alternatif, dictionary boleh diinisialisasi dengan nilai yang sama dalam notasi berikut:
phonebook = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
print(phonebook)
Mengulangi dictionary
Dictionary boleh diulang seperti senarai. Namun, tidak seperti senarai, dictionary tidak menyimpan susunan nilai yang disimpannya. Untuk mengulangi pasangan kunci dan nilai, gunakan sintaks berikut:
phonebook = {"John" : 938477566,"Jack" : 938377264,"Jill" : 947662781}
for name, number in phonebook.items():
print("Phone number of %s is %d" % (name, number))
Mengeluarkan nilai
Untuk mengeluarkan indeks yang ditentukan, gunakan salah satu notasi berikut:
phonebook = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
del phonebook["John"]
print(phonebook)
atau:
phonebook = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
phonebook.pop("John")
print(phonebook)
Exercise
Tambah "Jake" ke dalam buku telefon dengan nombor telefon 938273443, dan keluarkan Jill dari buku telefon.
phonebook = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
# your code goes here
# testing code
if "Jake" in phonebook:
print("Jake is listed in the phonebook.")
if "Jill" not in phonebook:
print("Jill is not listed in the phonebook.")
phonebook = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
# your code goes here
phonebook["Jake"] = 938273443
del phonebook["Jill"]
# testing code
if "Jake" in phonebook:
print("Jake is listed in the phonebook.")
if "Jill" not in phonebook:
print("Jill is not listed in the phonebook.")
test_output_contains("Jake is listed in the phonebook.")
test_output_contains("Jill is not listed in the phonebook.")
success_msg("Nice work!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!