2-D arrays
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · עמוד זה דורש דפדפן עדכני (תומך ב-SharedArrayBuffer). אנא עדכן את Chrome, Edge, Firefox או Safari לגרסה העדכנית ביותר.
English
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.
עברית
רשת של שורות ועמודות
- 2-D array היא רשימה שהפריטים שלה הם בעצם רשימות.
- היא מדגמת גריד או טבלה: שורות ועמודות.
- כל רשימה פנימית מייצגת שורה אחת.
grid = [[1, 2, 3],
[4, 5, 6]]
print(grid)
English
Reach one cell
- Use two indexes: first the row, then the column.
grid[row][col]— both count from 0 in Python.
עברית
הגעה לתא יחיד
- השתמש בשתי אינדקסים: קודם בשורה, ואז בעמודה.
grid[row][col]— שתי ספירות מתחילות מ0 ב-Python.
grid = [[1, 2, 3], [4, 5, 6]]
print(grid[0][1]) # 2
print(grid[1][2]) # 6
English
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.
עברית
לולאה מקורבת מבקרת את כל התאים
- הלולאה החיצונית עוברת את ה-שורות; הלולאה הפנימית עוברת את ה-עמודות.
for row in grid:מספק שורה אחת (רשימה) בכל פעם.
grid = [[1, 2], [3, 4]]
for row in grid:
for value in row:
print(value)
English
Add up a grid
- Keep a running
totaland add every cell with a nested loop.
עברית
חיבור טבלה
- שמור על
totalרצף והוסף כל תא באמצעות לולאה פנימית.
grid = [[1, 2, 3], [4, 5, 6]]
total = 0
for row in grid:
for value in row:
total = total + value
print(total) # 21
English
In the exam: ARRAY[1:rows, 1:cols]
- Pseudocode writes a 2-D array with two ranges, counting from 1.
עברית
במבחן: ARRAY[1:rows, 1:cols]
- כתיבת פסודוקוד יוצרת מערך דו-ממדי (2-D) עם שתי טווחים, סופר מהערך 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
English
Common mistakes
- Use two indexes:
grid[row][col]. - The outer loop walks the rows, the inner loop the columns.
עברית
טעויות נפוצות
- השתמש בשני אינדקסים:
grid[row][col]. - הלולאה החיצונית עוברת על השורות, ולולאה הפנימית על העמודות.
English
Now you try
- Use a nested loop or two indexes.
- Press Check answer to test your code.
עברית
כעת תנסו בעצמכם
- השתמש בלולאה מקורבת או בשני אינדקסים.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
Explore · חקור
A 2-D array is a grid
Rows of columns; reach a cell with grid[row][col].
From the grid, store the value in row 1, column 2 in a variable x (the answer is 6).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Add up every number in the 2-D list grid with a nested loop. Store the total in total (the answer is 21).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Use a nested loop to print a 3-by-3 square of stars: three lines, each with three * characters.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.