Informatics 9. Билингвальный учебник - [15]
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”
b) for i = 1 to 10:
print “hi”
c) for i in 1 - 10:
print “hi”
d) for i from 0 to 9:
print “hi”
5. Which of the following best describes the purpose of a for loop?
a) A for loop is for doing something an indeterminate number of times.
b) A for loop is doing something an infinite number of times.
c) A for loop is for doing something a fixed number of times.
d) A for loop is for doing something three times.
6. Which Python keyword skips back to the beginning of a loop?
a) break
b) continue
7. Which Python keyword exits a loop?
a) break
b) continue
8. What does the following program print?
for i in range(2):
for j in range(2):
print i + j
a) 0
1
1
2
b) 0112
c) 0
1
0
1
d) 0101
9. How many lines does the following program print?
for i in range(3):
for j in range(5):
print “hi”
a) 3
b) 5
c) 8
d) 15
10. Which of the following while loops would continue to loop as long as num is in the range 3 to 12, exclusive?
a) while num > 12 and num < 3:
# do something with num
b) while num < 12 or num > 3:
# do something with num
c) while num <= 12 and num >= 3:
# do something with num
d) while num < 12 and num > 3:
# do something with num
11. What is the value of sum when this loop completes?
sum = 0
for i in range(3):
sum = sum + 5
for j in range(2):
sum = sum - 1
a) 8
b) 9
c) 20
d) 0
12. Which of the following for loops would print the following numbers?
3
5
7
9
a) for i in range(3, 10, 2):
print i
b) for i in range(3, 9, 2):
print i
c) for i in range(9):
print i
d) for i in range(3,9):
print i
13. Which of the following for loops would print the following numbers?
0
1
2
3
4
5
a) for i in range(5):
print i
b) for i in range(1, 5, 1):
print i
c) for i in range(6):
print i
d) for i in range(0,5, 1):
print i
14. What does this program print?
for i in range(6):
if i == 3:
continue
print i
a) 0
1
2
b) 0
1
2
3
c) 0
2
4
6
d) 0
1
2
4
5
15. Which of the following Python programs creates a list with the numbers 1 through 5?
a) my_list = (1, 2, 3, 4, 5)
b) my_list = [1, 2, 3, 4, 5]
c) my_list = 1, 2, 3, 4, 5
d) my_list = “1, 2, 3, 4, 5”
16. Look at the following program:
my_list = [“bananas”, “oranges”, “grapes”,
“pineapples”, “apples”]
# You pick the code that goes here...
# ...
# ...
print my_list
Pick the code that results in the following output:
[‘apples’, ‘bananas’, ‘grapes’, ‘oranges’, ‘pineapples’]
a) my_list.sort()
my_list.reverse()
b) my_list.sort()
c) my_list.reverse()
d) my_list.remove(“grapes”)
17. What does this program print?
my_list = [-4, 2, 3, 2, -2, 5]
print [x % 2 == 0 for x in my_list]
a) [True, True, False, True, True, False]
b) False
c) [0, 0, 1, 0, 0, 1]
d) [-4, 2, 2, -2]
18. Which of the following lines of code will cause an error?
Use the following definition of ages:
ages = (12, 5, 8)
a) ages = ages + (1, 3, 5)
b) print ages[2]
c) ages = ages[2:]
d) ages[0] = 3
19. What does this code snippet print?