2-D arrays · Array 2-D
A grid of rows and columns
- A 2-D array is a list whose items are themselves lists.
- It models a grid or table: rows of columns.
- Each inner list is one row.
Grid baris dan kolom
- Array 2-D adalah daftar yang isinya sendiri-sendiri adalah daftar.
- Ia memodelkan grid atau tabel: baris dari kolom-kolom.
- Setiap daftar dalam adalah satu baris.
grid = [[1, 2, 3],
[4, 5, 6]]
print(grid)
Reach one cell
- Use two indexes: first the row, then the column.
grid[row][col]— both count from 0 in Python.
Akses satu sel
- Gunakan dua indeks: pertama baris, kemudian kolom.
grid[row][col]— keduanya dihitung dari 0 di Python.
grid = [[1, 2, 3], [4, 5, 6]]
print(grid[0][1]) # 2
print(grid[1][2]) # 6
A nested loop visits every cell
- The outer loop walks the rows; the inner loop walks the columns.
for row in grid:gives one row (a list) at a time.
Perulangan bersarang mengunjungi setiap sel
- Loop luar menelusuri baris; loop dalam menelusuri kolom.
for row in grid:memberikan satu baris (sebuah daftar) sekaligus.
grid = [[1, 2], [3, 4]]
for row in grid:
for value in row:
print(value)
Add up a grid
- Keep a running
totaland add every cell with a nested loop.
Jumlahkan grid
- Pegang
totalberjalan dan tambahkan setiap sel dengan perulangan bersarang.
grid = [[1, 2, 3], [4, 5, 6]]
total = 0
for row in grid:
for value in row:
total = total + value
print(total) # 21
In the exam: ARRAY[1:rows, 1:cols]
- Pseudocode writes a 2-D array with two ranges, counting from 1.
Dalam ujian: ARRAY[1:rows, 1:cols]
- Pseudocode menulis array 2-D dengan dua rentang, menghitung dari 1.
DECLARE Grid : ARRAY[1:2, 1:3] OF INTEGER
FOR R ← 1 TO 2
FOR C ← 1 TO 3
OUTPUT Grid[R, C]
NEXT C
NEXT R
Common mistakes
- Use two indexes:
grid[row][col]. - The outer loop walks the rows, the inner loop the columns.
Kesalahan umum
- Gunakan dua indeks:
grid[row][col]. - Loop luar melintasi baris, loop dalam melintasi kolom.
Now you try
- Use a nested loop or two indexes.
- Press Check answer to test your code.
Sekarang Anda coba
- Gunakan perulangan bersarang atau dua indeks.
- Tekan Periksa jawaban untuk menguji kode Anda.
A 2-D array is a grid · Array 2-D adalah sebuah grid
Rows of columns; reach a cell with grid[row][col]. · Baris kolom; akses sel dengan grid[row][col].
From the grid, store the value in row 1, column 2 in a variable x (the answer is 6). · Dari grid, simpan nilai di baris 1, kolom 2 dalam variabel x (jawabannya 6).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Add up every number in the 2-D list grid with a nested loop. Store the total in total (the answer is 21). · Jumlahkan setiap angka dalam daftar 2-D grid dengan perulangan bersarang. Simpan totalnya dalam total (jawabannya adalah 21).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Use a nested loop to print a 3-by-3 square of stars: three lines, each with three * characters. · Gunakan perulangan nested untuk mencetak kotak bintang 3x3: tiga baris, masing-masing dengan tiga karakter *.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.