1-D arrays (lists) · Array 1-D (daftar)
A list holds many values
- A list (the exam calls it an array) stores many values under one name.
- Write the items in square brackets, separated by commas.
- An array normally stores items of the same type.
Daftar menyimpan banyak nilai
- Daftar (disebut array dalam ujian) menyimpan banyak nilai di bawah satu nama.
- Tulis item dalam kurung siku, dipisahkan koma.
- Array biasanya menyimpan item dengan tipe yang sama.
scores = [40, 55, 72, 90]
print(scores)
Reach an item by its index
- Each item has a position number called its index.
- Python counts from 0, so the first item is
scores[0].
Akses item berdasarkan indeksnya
- Setiap item memiliki nomor posisi yang disebut indeks.
- Python menghitung dari 0, sehingga item pertama adalah
scores[0].
scores = [40, 55, 72, 90]
print(scores[0]) # 40
print(scores[2]) # 72
Index and change a list · Indeks dan ubah daftar
Items are numbered from 0; scores[0] is the first item. · Item dinomori dari 0; scores[0] adalah item pertama.
Change an item
- Assign to an index to replace that one item.
Ubah item
- Berikan nilai pada indeks untuk mengganti item tersebut.
scores = [40, 55, 72, 90]
scores[1] = 99
print(scores) # [40, 99, 72, 90]
Length and looping
len(scores)gives how many items there are.- A
forloop visits each item in turn.
Panjang dan perulangan
len(scores)memberikan berapa banyak item yang ada.- Perulangan
formengunjungi setiap item secara bergantian.
scores = [40, 55, 72, 90]
print(len(scores)) # 4
for s in scores:
print(s)
In the exam: arrays start at 1
- Pseudocode declares an array with a size, and usually counts from 1.
Dalam ujian: array dimulai dari 1
- Pseudocode mendeklarasikan array dengan ukuran, dan biasanya menghitung dari 1.
DECLARE Scores : ARRAY[1:4] OF INTEGER
Scores[1] ← 40
OUTPUT Scores[1]
Common mistakes
- Index from
0; the last item is at positionlength - 1. - Going past the end gives an index error.
Kesalahan umum
- Indeks mulai dari
0; item terakhir berada di posisilength - 1. - Melewati akhir akan menyebabkan error indeks.
Now you try
- Use an index, or a loop, on each list.
- Press Check answer to test your code.
Sekarang Anda coba
- Gunakan indeks, atau perulangan, pada setiap daftar.
- Tekan Periksa jawaban untuk menguji kode Anda.
The list colours is set for you. Store its second item in a variable called second (remember Python counts from 0). · Daftar colours telah disediakan. Simpan item kedua-nya dalam variabel bernama second (ingat Python menghitung dari 0).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Change the first item of the list nums to 99. · Ubah item pertama dari daftar nums menjadi 99.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Use a loop to add up every number in nums and store the result in total (the answer is 43). · Gunakan perulangan untuk menjumlahkan setiap angka dalam nums dan simpan hasilnya dalam total (jawabannya adalah 43).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.