Lists and data abstraction · List dan abstraksi data
Why a list?
- A program often needs many values at once, like a class of scores.
- One variable per value would be messy.
- A list holds many values under one name — a useful data abstraction.
Mengapa daftar?
- Program sering membutuhkan banyak nilai sekaligus, seperti kelas skor.
- Satu variabel per nilai akan berantakan.
- Sebuah daftar menyimpan banyak nilai di bawah satu nama—sebuah abstraksi data yang berguna.
What is a data abstraction?
- An abstraction hides detail so you can think about the whole, not the parts.
- A list lets you treat 1 value or 1000 values the same way.
- You write the same code no matter how long the list is.
Apa itu abstraksi data?
- Sebuah abstraksi menyembunyikan detail agar Anda dapat memikirkan keseluruhan, bukan bagian-bagiannya.
- Daftar memungkinkan Anda memperlakukan 1 nilai atau 1000 nilai dengan cara yang sama.
- Anda menulis kode yang sama tidak peduli seberapa panjang daftarnya.
Make a list and read it
- Write items inside square brackets:
[10, 20, 30]. - Get one item by its index (its position). Python starts at
0. scores[0]is the first item;scores[-1]is the last item.
Buat dan baca daftar
- Tulis item di dalam kurung siku:
[10, 20, 30]. - Ambil satu item berdasarkan indeks (posisinya). Python dimulai dari
0. scores[0]adalah item pertama;scores[-1]adalah item terakhir.
scores = [10, 20, 30]
print(scores[0])
print(scores[-1])
print(len(scores))
Grow a list with append
scores.append(x)addsxto the end of the list.- The list can grow while the program runs.
len(scores)tells you how many items it now has.
Tambahkan daftar dengan append
scores.append(x)menambahkanxke akhir daftar.- Daftar dapat bertambah saat program berjalan.
len(scores)memberi tahu berapa banyak item yang dimilikinya sekarang.
scores = [10, 20]
scores.append(30)
print(scores)
print(len(scores))
Traverse (visit every item)
- To traverse a list means to go through every item, one by one.
for s in scores:gives you one item each pass — this is a for-each loop.- It is the clean way to process the whole list.
Traversal (unjungi setiap item)
- Untuk traversing daftar berarti melewati setiap item, satu per satu.
for s in scores:memberikan satu item setiap kali lewat—ini adalah loop for-each.- Ini adalah cara bersih untuk memproses seluruh daftar.
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
In AP CSP pseudocode
- A list is the exam's list too — but CB lists are 1-indexed.
- So
aList[1]is the first element in CB, while Python's first isscores[0]. FOR EACH x IN listmatches Python'sfor x in list:.DISPLAY= print,←==.
Dalam pseudocode AP CSP
- Daftar adalah daftar ujian juga—tetapi daftar CB 1-indexed.
- Jadi
aList[1]adalah elemen pertama di CB, sementara pertama Python adalahscores[0]. FOR EACH x IN listcocok dengan Python'sfor x in list:.DISPLAY= print,←==.
aList ← [10, 20, 30]
DISPLAY aList[1] ## the FIRST item (1-indexed!)
total ← 0
FOR EACH x IN aList
{
total ← total + x
}
DISPLAY total
Common mistakes
- Index a list from
0; the last item isa[-1]. - A list groups related values under one name.
Kesalahan umum
- Indeks daftar dari
0; item terakhir adalaha[-1]. - Daftar mengelompokkan nilai-nilai terkait di bawah satu nama.
Now you try
- Build, grow, or traverse a list in each task.
- Press Check answer to test your code.
Sekarang Anda coba
- Bangun, tambahkan, atau traverse daftar dalam setiap tugas.
- Tekan Periksa jawaban untuk menguji kode Anda.
A list of items, indexed · Daftar item, diindeks
A list stores many values in order, each at an index from 0. · List menyimpan banyak nilai secara berurutan, masing-masing pada index dari 0.
Start from the list colors = ["red", "green"]. Use .append(...) to add "blue" to the end, so the list becomes ["red", "green", "blue"]. · Mulai dari daftar colors = ["red", "green"]. Gunakan .append(...) untuk menambahkan "blue" ke bagian akhir, sehingga daftarnya menjadi ["red", "green", "blue"].
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
nums is [4, 8, 15, 16, 23]. Traverse it with a for loop to add up every value, then print the total. (It should print 66.) · nums adalah [4, 8, 15, 16, 23]. Traversal dengan perulangan for untuk menjumlahkan setiap nilai, lalu print totalnya. (Seharusnya mencetak 66.)
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]. Store the first day in first and the last day in last using indexing. · days = ["Mon", "Tue", "Wed", "Thu", "Fri"]. Simpan hari pertama dalam first dan hari terakhir dalam last menggunakan indeksasi.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.