Creating an array using a list - Answers
Q1. Study the code!
myArray=[[1,2,3],[4,5,6],[7,8,9]]
print(myArray)
for i in range(3):
for j in range(3):
print(myArray[i][j],end=' ')
print('\n')
Q2. myArray has 3 elements.
Q3. The first element is at position 0.
Q4. The last element is at position 2.
Q5. What will get printed out - a grid 3 by 3 of the first 9 integers starting at 1:
1 2 3
4 5 6
7 8 9
Q6. Run the code and see if your prediction from the last question was correct.
Q7. To print out index position 0 of element 2 in myArray, use
temp = myArray[2][0]
print (temp)
Q8. Change element 2 of the first sublist to be 333. Print out myArray.
myArray[0][1]=333
print(myArray)
Q9. Write a program to print out a nicely present grid of the first 16 integers starting at 1 using a list that has 4 elements, each element being a sublist of 4 numbers.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
myArray=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16] ]
print(myArray)
for i in range(4):
for j in range(4):
print(myArray[i][j],end='\t ')
print('\n')
Q10. To change the number 14 so that it is 77:
myArray[3][1]=77
for i in range(4):
for j in range(4):
print(myArray[i][j],end='\t ')
print('\n')