Dictionaries (records) · Diksi (rekaman)
Store data by name
- A dictionary stores values under names called keys.
- Write pairs inside braces:
{"name": "Sam", "age": 15}. - Each key is unique and points to one value.
Menyimpan data dengan nama
- Dictionari menyimpan nilai di bawah nama yang disebut key.
- Tulis pasangan di dalam kurung kurawal:
{"name": "Sam", "age": 15}. - Setiap key unik dan mengarah ke satu nilai.
Get and set values
- Read a value with its key:
person["name"]. - Add or change a value the same way:
person["age"] = 16. - A list uses a number index; a dictionary uses a key.
Mengambil dan menetapkan nilai
- Baca nilai dengan kuncinya:
person["name"]. - Tambahkan atau ubah nilai dengan cara yang sama:
person["age"] = 16. - List menggunakan indeks angka; dictionari menggunakan key.
ages = {"Sam": 12, "Mia": 15}
print(ages["Sam"])
ages["Leo"] = 9
print(ages)
A dictionary as a record
- A record groups related facts about one thing.
- A dictionary is a neat way to hold a record.
- Each key is a field, like
nameorgrade.
Dictionari sebagai record
- Record mengelompokkan fakta-fakta terkait tentang satu hal.
- Dictionari adalah cara rapi untuk menyimpan record.
- Setiap key adalah field, seperti
nameataugrade.
pupil = {"name": "Ada", "grade": "A"}
print(pupil["name"])
print(pupil["grade"])
Check and loop
"name" in personisTrueif that key exists.for key in person:visits each key in turn.person.get("x", 0)returns a default if the key is missing.
Memeriksa dan mengulang
"name" in personbernilaiTruejika key tersebut ada.for key in person:mengunjungi setiap key secara bergantian.person.get("x", 0)mengembalikan nilai default jika key hilang.
menu = {"tea": 2, "cake": 5}
for item in menu:
print(item, menu[item])
print("tea" in menu)
In Cambridge pseudocode
- A dictionary models the exam's
TYPErecord; each key is a field reached with a dot.
Dalam pseudocode Cambridge
- Dictionari memodelkan record
TYPEujian; setiap key adalah field yang diakses dengan titik.
TYPE Student
DECLARE Name : STRING
DECLARE Age : INTEGER
ENDTYPE
DECLARE Pupil : Student
Pupil.Name ← "Sam"
Pupil.Age ← 15
Common mistakes
- Read a value by its key:
d["name"]; a missing key raises a KeyError. - Keys are unique — assigning the same key again overwrites the value.
- Use
d.get(key)when the key might be missing.
Kesalahan umum
- Baca nilai berdasarkan kuncinya:
d["name"]; key yang hilang akan menyebabkan KeyError. - Key bersifat unik — menetapkan key yang sama lagi akan menimpa nilainya.
- Gunakan
d.get(key)ketika key mungkin hilang.
Now you try
- Build or update a dictionary in each task.
- Press Check answer to test your code.
Sekarang Anda coba
- Bangun atau perbarui dictionari di setiap tugas.
- Tekan Periksa jawaban untuk menguji kode Anda.
Look up by key · Cari berdasarkan kunci
A dictionary jumps straight to the value for a key — no scanning. · Diksi melompat langsung ke nilai untuk suatu kunci — tanpa pemindaian.
Create a dictionary book with two keys: title set to "Python" and pages set to 200. · Buat diksi book dengan dua kunci: title diatur ke "Python" dan pages diatur ke 200.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
The dictionary student already has name and score. Change score to 85, and add a new key passed set to True. · Diksi student sudah memiliki name dan score. Ubah score ke 85, dan tambahkan kunci baru passed diatur ke True.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Loop over the dictionary prices and add up its values. Store the total in total (the answer is 10). · Loop melalui diksi prices dan jumlahkan nilainya. Simpan total dalam total (jawabannya 10).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Write count_words(text) that returns a dictionary: each word of text (split on spaces) mapped to how many times it appears. count_words("the cat and the hat") gives {"the": 2, "cat": 1, "and": 1, "hat": 1}. · Tulis count_words(text) yang mengembalikan diksi: setiap kata dari text (dipisah spasi) dipetakan ke berapa kali muncul. count_words("the cat and the hat") menghasilkan {"the": 2, "cat": 1, "and": 1, "hat": 1}.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.