print(y, end = “ ”)
print()
Когда приведенный выше код выполняется, он дает следующий результат.
Output
11 12 5 2
15 6 10
0 5 11 13 6
10 8 12 5
Updating Values in Two-Dimensional Array
We can update the entire inner array or some specific data elements of the inner array by reassigning the values using the array index.
Example 2
List = [[11, 12, 5, 2], [15, 6, 10], [10, 8, 12, 5], [12,15,8,6]]
List[2] = [11,9]
List[0][3] = 7
for x in List:
for y in x:
print(y, end = “ ”)
print()
Output
11 12 5 7
15 6 10
11 9
12 15 8 6
Practice 1
The list given below is the сalendar for May.
1. Insert this information to the two-dimensional list;
2. Find the national holidays of Republic Kazakhstan and replace them with the word “Holiday”.
Deleting the Values in Two-Dimensional Array
We can delete the entire inner array or some specific data elements of the inner array by reassigning the values using the del() method with index. But in case you need to remove specific data elements in one of the inner arrays, then use the update process described above.
Example 3
List = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
del List[3]
for x in List:
for y in x:
print(y, end = “ “)
print()
Output
11 12 5 2
15 6 10
10 8 12 5
Keep in mind
We can use this type of data structure to encode information about an image. For example, the following grayscale image could be represented by the following list:
x=[[236, 189, 189, 0],
[236, 80, 189, 189],
[236, 0, 189, 80],
[236, 189, 189, 80]]
Practice 2
1. Create 4Ч5 two-dimensional list;
2. Insert 0s and 1s to the list according to the letter shown in figure right;
3. Insert 0s where it’s empty and 1s where it’s filled in the figure;
4. Update list values so that to change letter K to letter O.
Literacy
1. What is the difference between inserting value and updating value?
2. What we should do to delete only one element from an inner array, not the entire row?
Terminology
value – мəні – значение
execute – орындау – выполнять
reassign – қайта мəн беру – переназначить
inner – ішкі – внутренний
CHECK YOURSELF
1. What type is the following variable?
x = “Hi there”
a) float
b) integer
c) boolean
d) string
2. How many lines will this program print?
while True:
print “hi”a) 0
b) 10
c) 100
d) an infinite number of lines
3. How many lines will this program print?
x = 10
while x > 0:
print x
x = x - 3
a) 3
b) 4
c) 5
d) 6
4. Which of the following programs prints ten lines?
a) for i in range(10):
print “hi”