0% found this document useful (0 votes)
30 views

Chapter 8 - Array

The document discusses arrays and multidimensional arrays in Python. It begins by defining an array as a list of more than one variable of the same data type that can hold multiple values. Arrays allow accessing elements using an index that starts at 0. The document then provides examples of one-dimensional arrays and explains how to access elements. It also discusses storing user input in arrays and processing array values. The document concludes by defining multidimensional arrays as arrays within arrays, and how elements can be accessed using row and column indexes and for loops.

Uploaded by

aayana angel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Chapter 8 - Array

The document discusses arrays and multidimensional arrays in Python. It begins by defining an array as a list of more than one variable of the same data type that can hold multiple values. Arrays allow accessing elements using an index that starts at 0. The document then provides examples of one-dimensional arrays and explains how to access elements. It also discusses storing user input in arrays and processing array values. The document concludes by defining multidimensional arrays as arrays within arrays, and how elements can be accessed using row and column indexes and for loops.

Uploaded by

aayana angel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Chapter 8

ARRAY
Dr Suryanti Awang, Faculty of Computing
Outcome

 To understand the concept of array and multidimensional array


 To implement the concept in a program
 To implement the concept in functions program
 To implement the concept in matrices operation

 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

Dr Suryanti Awang, Faculty of Computing


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
 Example : age
age = [23,32,18,20,35] age[0] 23

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

normal variable: array variable:

1 variable name → Data type: string


Data type: string hold more than one
value at a time
Dr Suryanti Awang, Faculty of Computing
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

Dr Suryanti Awang, Faculty of Computing


Introduction
 The values inside the array element can be accessed by referring the index
number
 Index number started at 0

normal variable: array variable:

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 :

Function len() to get


length of the matricID → Let’s
length is 3 Start access the array debug
from index=0, 1 and 2

Dr Suryanti Awang, Faculty of Computing


User enter input to array list

 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

Dr Suryanti Awang, Faculty of Computing


User enter input to array list
User define length of the list; numStd

Create an array list named matricID and


define the size based on the length in
numStd, 0 is the first index in the array list

Enter element of the array list in for loop


based on the defined length

Let’s
debug

Dr Suryanti Awang, Faculty of Computing


Example 1 – enter id num, quiz 1,
 User able to enter student ID and quiz mark.

Let’s
debug

matricID quiz

matricID[0] KA19067 quiz[0] 15.5


matricID[1] AA20398 quiz[1] 14.5

Dr Suryanti Awang, Faculty of Computing


Process the array value

 To determine average
quiz mark.

Let’s
debug

Dr Suryanti Awang, Faculty of Computing


Exercise 1-1: Array

 Develop an array list to enter student name and student age for a certain
number of students.

Dr Suryanti Awang, Faculty of Computing


Exercise 1-2: Array
 Store the values in array lists.

Sample Output:

Array list:
val1,
val2,
total

Dr Suryanti Awang, Faculty of Computing


Multidimensional Array
 2-dimensional array
 An array within an array
 The values inside the array element can be accessed by referring the index
number in a row and columns
 Example; temperatures are recorded 3 times a day in 4 days → 4 days can
be represented as 2D array with 3 recorded temperatures in each day.
Day 1 – 32 36 31
Day 2 – 34 35 36
Day 3 – 37 38 38 column
Day 4 – 38 37 36
[0] [1] [2]

temp[0] 32 36 31
temp[1] 34 35 36
row
temp[2] 37 38 38

Dr Suryanti Awang, Faculty of Computing


temp[3] 38 37 36
Multidimensional Array
 How to access the value in the array elements? →index by row and column

column

[0] [1] [2]

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]

Dr Suryanti Awang, Faculty of Computing


Multidimensional Array
 How to access the value in the array elements? →index by row and column

row column
column

[0] [1] [2]

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]

Dr Suryanti Awang, Faculty of Computing


Multidimensional Array
 How to access the value in the array elements? →index by row and column

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

Dr Suryanti Awang, Faculty of Computing


Multidimensional Array
 How to access the value in the array elements? →index by row and 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

Dr Suryanti Awang, Faculty of Computing


Multidimensional Array
 What if the row and column length is not fixed? → must get the length first.

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

Dr Suryanti Awang, Faculty of Computing


User enter input to 2D array list

 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

Dr Suryanti Awang, Faculty of Computing


User enter input to 2D array list Let’s
debug

temp is an array list:


- The outside bracket [ ] is
the row based on number
of days (day)
- The inside bracket [ ] is the
column based on number
of temp (numTemp)

The first ‘for loop’ is for the row

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

Dr Suryanti Awang, Faculty of Computing


Multidimensional Array
 Process the values → calculate average temp for each day
Let’s
debug

Dr Suryanti Awang, Faculty of Computing


Example 2 – 2D Array

1. Change this array list to become 2D


array → instead of only for quiz, this
array can store a certain number of
assessment for each student → name
it as assessment
2. The row is number of students, the
column is number of assessment

Dr Suryanti Awang, Faculty of Computing


Example 2 – 2D Array Let’s
debug

Dr Suryanti Awang, Faculty of Computing


Exercise 2-1: 2D Array

- Change val1 and val2 into 2D


array, name it as value
- The length size of value is based
on user input
- Row → size of number of total
value
- Col → size of number of value to
be summed

Dr Suryanti Awang, Faculty of Computing


Exercise 2-1: 2D Array

Sample Output:

Dr Suryanti Awang, Faculty of Computing


Implement array in functions technique
 Passing an array list to functions
- Build a function named
calculate_Mark

Dr Suryanti Awang, Faculty of Computing


Implement array in functions technique
 Passing an array list to functions

Let’s
debug

Dr Suryanti Awang, Faculty of Computing


Implement array in functions technique
 Passing 2D array list to functions

Dr Suryanti Awang, Faculty of Computing


Implement array in functions technique
 Passing 2D array list to functions

Let’s
debug

Dr Suryanti Awang, Faculty of Computing


Example 3 – pass array to functions
 Recall → chapter 7, example of calculate total value

Dr Suryanti Awang, Faculty of Computing


Implement
array in
functions function calculate_Value
in calculate file 1
technique

function
determine_Grade in
grade file 1

Dr Suryanti Awang, Faculty of Computing


Implement Build function calculate_Value in calculate
file 2
array in - Parameter:
- values: array list consists of values that
functions user enter
- rowSize: row length→number of total
technique values
- colSize: col length→number of values

Return totalValues in array list Statement to calculate total based on


how many values user enter for each row
Build function determine_Grade
Implement - Parameter:
- values: array list consists of values that user enter
2

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

Import function inside grade file 3


technique

Let’s Call function calculate_Value is


debug by passing the array list named 4
value, int numTotal and numVal

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

Dr Suryanti Awang, Faculty of Computing


Array in Matrices Operation
Import library NumPy

Create matrix x and y using


function array() in numpy

Matrices operations

Dr Suryanti Awang, Faculty of Computing


Dr Suryanti Awang, Faculty of Computing
Exercise (Easy Level) – robot arm
Enhance the robot arm program by implementing array list:
- The theta and length values must be in array list based on how many number of
robot arm entered by user in numRobot
- Pass the array list to the functions: determine_Coor() and
coordinate_Location()

Dr Suryanti Awang, Faculty of Computing


Exercise (Intermediate Level) – tax system
Enhance the tax system program in chapter 7 by implementing any 2D array list:
- Use that 2D array list to store employees info (employee ID, employee Name and
taxable yearly salary).
- Pass the 2D array value into the related functions

To enter the
employees info in this
loop and store in 2D
array empInfo

Pass the array value to


the functions

Dr Suryanti Awang, Faculty of Computing


Exercise (Hard Level) – calculate purchase
discount

Enhance the calculate purchase discount program in chapter 7 by implementing any


appropriate array list:
- Use that array list to pass the value into the related functions

Dr Suryanti Awang, Faculty of Computing


Dr Suryanti Awang, Faculty of Computing

You might also like