week9 Flashcards
Array
py
- An array is a data structure containing several elements of the same data type; these elements can be accessed using the same identifier name.
- The position of each element in an array is identified using the array’s index.
- Index of the first element in an array: Lower Bound
- Index of the last element in an array: Upper Bound
(Python doesn’t use arrays, so we will use lists when coding)
colours = ["red", "green", "blue"] print(colours[0])
It is also possible to output the whole list without accessing the list indexes by including the name of the list within the print statement:
print(colours)
List slicing
py
We can slice lists in the same way we slice strings:
~~~
animal = [“pink”,”flamingo”,”grey”,”koala”]
print(animal[0:2])
print(animal[2:4])
~~~
output
~~~
[“pink”, “flamingo”]
[“grey”, “koala”]
~~~
Iterating through arrays
py
mylist = [1,2,3,4,5] counter = 0 while counter < len(mylist): print(mylist[counter]) counter += 1
alt. my own
~~~
mylist = [1,2,3,4,5]
for element in mylist:
print(element)
~~~
2D list
py
A list of lists
mylist = [[a,a,a],[b,b,b]] print(list[row][column])
Arrays
summary
An array is a data structure containing several elements of the same data type; these elements can be accessed using the same identifier name. The position of each
element in an array is identified using the array’s index. The index of the first element in an array is the lower bound and the index of the last element is the upper bound.
The lower bound of an array is usually set as zero or one. Some programming
languages can automatically set the lower bound of an array. Arrays can be one-dimensional or multi-dimensional.
array
pseudocode
A 1D array can be referred to as a list. When a 1D array is declared in pseudocode, the lower bound (LB), upper bound (UB) and data type are included:DECLARE <identifier> : ARRAY[LB:UB] OF <data type>
For example:DECLARE myList : ARRAY[0:8] OF INTEGER
The declared array can then be used, as follows:myList[7] ← 16
2D arrays
pseudocode
A 2D array can be referred to as a table, with rows and columns.
When a 2D array is declared in pseudocode, the lower bound for rows (LBR) and the upper bound for rows (UBR), the lower bound for columns (LBC) and the upper bound for columns (UBC), and data type are included:DECLARE <identifier> : ARRAY[LBR:UBR, LBC:UBC] OF <data type>
For example:DECLARE myArray : ARRAY[0:8,0:2] OF INTEGER
The declared array can then be used, as follows:myArray[7,0] ← 16
File Handling
Files are needed to store data.
When using files in our code, they can be opened in one of the following modes:
READ – reads data from a file
WRITE – writes data to the file, existing data will be overwritten
APPEND – adds data to the end of the file
When a file is no longer being used it should be closed.
write text to a file
py
output = 'This is some text' file = open('myfile.txt', 'w') file.write(output) file.close()
Use concatenate before writing to a file
py
output = 'This is some text' output = output + '\nAnd this is some more text' file = open('myfile.txt', 'w') file.write(output) file.close()
write a list to a file
py
shoppingList = ['bread', 'jam', 'butter'] file = open('myfile.txt', 'w') for item in shoppingList: file.write(item + '\n') file.close()
read entire contents of a file
py
file = open('myfile.txt', 'r') output = file.read() print(output) file.close()
read each line from a file
py
file = open('myfile.txt', 'r') for line in file: print(line) file.close()
read each line into a list
py
file = open('myfile.txt', 'r') lines = [] for line in file: lines.append(line[:len(line)-1]) file.close() print(lines)
catch errors when reading file
try: file = open('myfile.txt', 'r') output = file.read() print(output) file.close() except IOError as e: print('Cannot open file. Please check the file exists and is closed.')