8.2 Arrays
8.2 Arrays
• We can display the data that lies in a particular location in an array as follows:
• The declared array can then be populated using a loop, just like for one-dimensional
arrays – however this time there need to be two nested loops, one for each index:
• We can display the data that lies in a particular location in a 2D array as follows:
The output will be 98
Note!!!
Instead of arrays, Python uses another object called
a list. The differences you need to know are:
• a list can contain different data types whereas
arrays must all hold the same type of data
• to achieve the same structure as a two-dimensional
array, Python embeds lists within another.
Two-dimensional arrays
• Two-dimensional arrays are populated in python as follows:
Activity 8.23
In python, write a program to declare and populate
the array MyTable, as shown in the Figure below,
using a nested FOR loop.
Activity 8.23
Python
#array with nothing print(MyList)
MyList = [] print(MyList[5,5])
#loop to enter the numbers to the array
for Counter1 in range(3):
Column = []
for Counter2 in range(10):
Column.append(int(input("Enter your number:")))
MyList.append(Column)