Lists (1-D arrays) · Daftar (array 1-D)
A list holds many values
- A list keeps many values in one variable.
- Write the items inside square brackets:
[10, 20, 30]. - The items can be numbers, strings, or a mix.
Daftar menyimpan banyak nilai
- List menyimpan banyak nilai dalam satu variabel.
- Tulis item di dalam kurung siku:
[10, 20, 30]. - Item bisa berupa angka, string, atau campuran keduanya.
Get and change items
- Use an index, just like a string:
nums[0]is the first item. nums[-1]is the last item.- You can also change an item:
nums[0] = 99.
Mengambil dan mengubah item
- Gunakan indeks, sama seperti string:
nums[0]adalah item pertama. nums[-1]adalah item terakhir.- Anda juga bisa mengubah sebuah item:
nums[0] = 99.
fruits = ["apple", "pear", "plum"]
print(fruits[0])
print(fruits[-1])
Add items and count them
nums.append(x)addsxto the end of the list.len(nums)tells you how many items there are.- A list can grow as your program runs.
Menambah dan menghitung item
nums.append(x)menambahkanxke akhir daftar.len(nums)memberitahu Anda berapa banyak item yang ada.- List dapat bertambah saat program berjalan.
fruits = ["apple", "pear"]
fruits.append("plum")
print(len(fruits))
print(fruits)
Loop over a list
for item in nums:visits each item in turn.- The loop variable holds one item per pass.
- This is the easy way to process every value.
Mengulang list
for item in nums:mengunjungi setiap item secara bergantian.- Variabel loop memegang satu item per kali lewat.
- Ini adalah cara mudah untuk memproses setiap nilai.
scores = [10, 20, 30]
total = 0
for s in scores:
total = total + s
print(total)
In Cambridge pseudocode
- A list is the exam's 1-D array. Note: exam arrays often start at index
1, but Python lists start at0.
Dalam pseudocode Cambridge
- List adalah array 1-D pada ujian. Catatan: array ujian sering dimulai dari indeks
1, tetapi list Python dimulai dari0.
DECLARE Scores : ARRAY[1:3] OF INTEGER
Scores[1] ← 10
Scores[2] ← 20
Scores[3] ← 30
FOR I ← 1 TO 3
OUTPUT Scores[I]
NEXT I
Common mistakes
- The first item is
a[0]and the last isa[-1];a[len(a)]is out of range. b = adoes not copy the list — both names share it. Usea.copy()to make a real copy.appendadds one item to the end.
Kesalahan umum
- Item pertama adalah
a[0]dan item terakhir adalaha[-1];a[len(a)]berada di luar jangkauan. b = atidak menyalin list — kedua nama berbagi list yang sama. Gunakana.copy()untuk membuat salinan sebenarnya.appendmenambah satu item di bagian akhir.
Now you try
- Build or loop over a list in each task.
- Press Check answer to test your code.
Sekarang Anda coba
- Bangun atau ulangi list di setiap tugas.
- Tekan Periksa jawaban untuk menguji kode Anda.
A list changes in place · Daftar berubah secara in-place
A list is ordered and indexed; append/change/pop all modify it. · Daftar bersifat terurut dan berindeks; append/change/pop semuanya memodifikasinya.
Create a list colors holding "red", "green", "blue". Then append "yellow" so the list has four items. · Buat daftar colors yang berisi "red", "green", "blue". Kemudian append "yellow" agar daftar memiliki empat item.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Loop over the list nums and add up its values. Store the total in total (the answer is 66). · Loop melalui daftar nums dan jumlahkan nilainya. Simpan total dalam total (jawabannya 66).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Find the largest value in nums with a loop. Start largest at the first item, then loop through nums and keep the bigger value. Store the answer in largest (it is 23). · Temukan nilai terbesar dalam nums menggunakan loop. Mulai largest dari item pertama, lalu loop melalui nums dan simpan nilai yang lebih besar. Simpan jawaban dalam largest (yaitu 23).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Build a new list backwards with the items of nums in reverse order. Use a loop, and do not change nums itself. · Buat daftar baru backwards dengan item-item dari nums dalam urutan terbalik. Gunakan loop, dan jangan ubah nums itu sendiri.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.