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

Module 7 - Part I: 1D Arrays and Lots of Brackets

The document discusses 1D and 2D arrays in Python. It covers how to create and initialize arrays, access and modify array elements, traverse arrays using for loops, define methods to find sums and averages of array elements, and work with 2D arrays using nested loops. Key points include initializing arrays when created in Python, using indexing like array_name[i] to access elements, and iterating over arrays to visit each element when traversing or modifying contents.

Uploaded by

Saadman Kabir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Module 7 - Part I: 1D Arrays and Lots of Brackets

The document discusses 1D and 2D arrays in Python. It covers how to create and initialize arrays, access and modify array elements, traverse arrays using for loops, define methods to find sums and averages of array elements, and work with 2D arrays using nested loops. Key points include initializing arrays when created in Python, using indexing like array_name[i] to access elements, and iterating over arrays to visit each element when traversing or modifying contents.

Uploaded by

Saadman Kabir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Module 7 – Part I

1D ARRAYS AND LOTS OF BRACKETS

4/10/2019 CSE 1321 MODULE 7 1


2. Creating and Initializing Arrays
You must choose:
1) Create an empty arrays and assign initial values with a loop
later on:
CREATE myArray[5]

OR

2) Create and initialize the array in one line. This is helpful when
we already know those value (e.g. days of the week, etc.).

4/10/2019 CSE 1321 2


Array Creation Example
We create arrays in Python by first importing the array module.
from array import *

Creation is different. Must include the typecode (‘i’ in the line below).
myArray = array('i',[10, 20, 30, 40, 50])
Typecode Value
b Represents signed integer of size 1 byte/td>
B Represents unsigned integer of size 1 byte
c Represents character of size 1 byte
i Represents signed integer of size 2 bytes
I Represents unsigned integer of size 2 bytes
f Represents floating point of size 4 bytes
d Represents floating point of size 8 byte
4/10/2019 CSE 1321 3
3. Accessing Information
• Copy information out of a particular slot
• Example:

CREATE clientAge // Integer


clientAge ← myArray[4]

• This copies information from the fifth slot


(slot four) into the variable clientAge

4/10/2019 CSE 1321 4


Horrible, but works…
// creates empty array with 5 slots, 0-4
CREATE myArray [5]

//Assigns literal values to each index in the array.


myArray[0] ← 10
myArray[1] ← 20
myArray[2] ← 30
myArray[3] ← 40
myArray[4] ← 50

4/10/2019 CSE 1321 5


Accessing and Modifying
#Accessing
secondNumber = myArray[1]
print(secondNumber)

#Modifying
myArray[3] = 42
print(myArray[3])

4/10/2019 CSE 1321 MODULE 4 6


Traversing the Array
You will use a loop to visit every cell of the array
Problem: create an array of 5 bytes and fill each
slot with the number 42
Solution:
CREATE myArray [5]
FOR i ← 0 to 4
myArray[i] ← 42
END FOR

4/10/2019 CSE 1321 7


Python – Traversing an Array
for x in myArray:
#do something. Assignment IS allowed!
x = x+10
print(x)

4/10/2019 CSE 1321 8


Another Array Example
from array import *
size = 5
myArray = array('i',[0] * size) # All zeroes
for x in myArray:
print(x)

# To fill with 10, 20, 30, 40 and 50...


for x in range(len(myArray)):
myArray[x] = (x + 1) * 10

4/10/2019 CSE 1321 9


Another Trace
CREATE smallestSoFar
smallestSoFar ← randomArray[0]
FOR counter ← 1 to 4
IF (smallestSoFar > randomArray[counter]) THEN
smallestSoFar ← randomArray[counter]
END IF
counter
END FOR
5
// Done
0 1 2 3 4 smallestSoFar

42 17 42 -8 4 -8
Finding the Minimum using a
Function
def function(*args):
temp = myArray[0]
for x in myArray:
if (x < temp):
temp = x
print(temp)

NOTE: Python is different. In Python, Object references are passed by value.


A function receives a reference to (and will access) the same object in memory as used by
the caller. However, it does not receive the box that the caller is storing this object in; as in
pass-by-value, the function provides its own box and creates a new variable for itself.

4/10/2019 CSE 1321 11


Finding the sum or average
using a method
METHOD FINDAVERAGE ( parameter: nums[])
BEGIN
sum ← 0
FOR i ← 0 to nums.length - 1
// MOST IMPORTANT LINE IS HERE
sum = sum + nums[i]
ENDFOR
average = sum / nums.length
return average
END FINDAVERAGE

4/10/2019 CSE 1321 12


Finding a sum and or average using a
method
def function(*args):
temp = 0
for x in myArray:
temp = temp + x
print("Average:", temp/len(myArray))
print("Sum: ", temp)

4/10/2019 CSE 1321 13


Defining a 2D array

CREATE array nums [numRows][numColumns]

4/10/2019 CSE 1321 14


Python – Define a 2D Array
In Python, 2D Arrays must be initialized when they are
created.

numbers = [[1,2,3,4,5], [6,7,8,9,10],


[11,12,13,14,15], [16,17,18,19,20]]

4/10/2019 CSE 1321 15


Working with 2D arrays
Usually involves nested loops, as shown below.
Problem Statement: Create an array of 4 rows and 5 columns.
Populate the array with the numbers 1-20.
---------------------------------------------------------------------
Create array grid[4][5]
count ← 1
FOR each element in a row
FOR each element in a column
grid[row][col] = count
count ← count + 1
END INNER FOR
END FOR

4/10/2019 CSE 1321 16


Working with 2D arrays

from array import *


numbers = [[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15],
[16,17,18,19,20]]
for r in numbers:
for c in r:
print(c, end = " ")
print()

#what value does this line print?


print(numbers[3][4])

4/10/2019 CSE 1321 17


Python - Working with Lists
colors = list()

#add colors
colors.append('purple')
colors.append('pink')
colors.append('green')

#find if list contains an element


if('pink' in colors):
print('pink is in the list')
print('\n')

#show the index of pink


print(colors.index('pink'))
print('\n')

#remove a color
colors.remove('green')

#output list
for x in range(len(colors)):
print (colors[x])

4/10/2019 CSE 1321 18

You might also like