Chapter 8 - Array
Chapter 8 - Array
ARRAY
Dr Suryanti Awang, Faculty of Computing
Outcome
More tutorial:
https://www.tutorialspoint.com/python/python_lists.htm
https://www.w3schools.com/python/python_arrays.asp
https://www.tutorialspoint.com/python_data_structure/python_2darray.htm
age[1] 32
Elements age[2] 18
age[3] 20
age[4] 35
Introduction
Array is a list of more than one variable having the same name and data
type
Hold more than one value at a time
The values inside the array element can be accessed by referring the index
number in a row
Index number started at 0
array variable:
matricID
matricID[0] AA19809
matricID[1] KA19056
matricID[2] KA20397
Let’s
Dr Suryanti Awang, Faculty of Computing Index start with 0 debug
Introduction
What if the values in the array up to 100? → from index 0, 1, 2 … 99
to access (input or output) the array → repetitive statement → for loop
For loop in array :
Steps:
1. Define length of the list → pre-define or ask user to define →
numStud
2. Create an array list with the length size based on step no 1 →
matricID = [0 for index in range(numStud)] → 0 is the first index
3. Use for loop statement to add value into the element of the array
list based on the defined length
Let’s
debug
Let’s
debug
matricID quiz
To determine average
quiz mark.
Let’s
debug
Develop an array list to enter student name and student age for a certain
number of students.
Sample Output:
Array list:
val1,
val2,
total
temp[0] 32 36 31
temp[1] 34 35 36
row
temp[2] 37 38 38
column
temp[0] 32 36 31 temp[0][0]
temp[1] 34 35 36
row temp[2][0]
temp[2] 37 38 38
temp[3] 38 37 36
temp[3][2]
row column
column
temp[0] 32 36 31 temp[0][0]
temp[1] 34 35 36
row temp[2][0]
temp[2] 37 38 38
temp[3] 38 37 36
temp[3][2]
row column
column for loop
[0] [1] [2]
temp[0] 32 36 31
temp[1] 34 35 36
row
temp[2] 37 38 38
temp[3] 38 37 36
for loop
temp[0] 32 36 31
temp[1] 34 35 36
row
temp[2] 37 38 38
temp[3] 38 37 36
for loop
Let’s
The second ‘for loop’ is debug
The first ‘for loop’ is for for column, function
row, function len() to len() to get length of
get length of the row → the column from each
the length is 4 row → the length is 3
Steps:
1. Define length of the list → pre-define or ask user to define → day is for the
row and numTemp is for the column
2. Create the 2D array list with the length size of the row and column based on
step no 1 → temp = [[0 for col in range(numTemp)] for row in range(day)]
→ 0 is the first index in the array list
3. Use nested for loop statement to add value into the element of the array list
based on the defined length of row and column
Input will be stored row by row The second ‘for loop’ is for the column
and by column. → second loop is executed until all
columns are executed before
proceed to the next row
Dr Suryanti Awang, Faculty of Computing
User enter input to 2D array list
Sample Output:
Let’s
debug
Let’s
debug
function
determine_Grade in
grade file 1
array in - total: array list of total that return from function calculate_Value
- rowSize: row length→number of total values
functions - colSize: col length→number of values
technique
Implement
array in 3
functions Import function inside calculate file
Call function 4
determine_Grade by
passing the array list
named value and
total, int numTotal
and numVal
Implement
array in
functions
technique
Array in Matrices Operation
Python library → NumPy → the core library for scientific computing
Functions of matrices operations:
add() → add elements of two matrices
subtract() → subtract elements of two matrices
divide() → divide elements of two matrices
multiply() → multiply elements of two matrices
dot() → product of two matrices
T → transpose a matrix
Matrices operations
To enter the
employees info in this
loop and store in 2D
array empInfo