week9 Flashcards

1
Q

Array

py

A
  • 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List slicing

py

A

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”]
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Iterating through arrays

py

A
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)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

2D list

py

A

A list of lists

mylist = [[a,a,a],[b,b,b]]
print(list[row][column])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Arrays

summary

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

array

pseudocode

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

2D arrays

pseudocode

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

File Handling

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

write text to a file

py

A
output = 'This is some text'

file = open('myfile.txt', 'w')
file.write(output)
file.close()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Use concatenate before writing to a file

py

A
output = 'This is some text'
output = output + '\nAnd this is some more text'

file = open('myfile.txt', 'w')
file.write(output)
file.close()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

write a list to a file

py

A
shoppingList = ['bread', 'jam', 'butter']

file = open('myfile.txt', 'w')

for item in shoppingList:
    file.write(item + '\n')
file.close()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

read entire contents of a file

py

A
file = open('myfile.txt', 'r')

output = file.read()

print(output)

file.close()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

read each line from a file

py

A
file = open('myfile.txt', 'r')

for line in file:
    print(line)

file.close()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

read each line into a list

py

A
file = open('myfile.txt', 'r')

lines = []

for line in file:
    lines.append(line[:len(line)-1])

file.close()

print(lines)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

catch errors when reading file

A
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.')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

new line in a file

A

“\n”

17
Q

in the statement file = open('myfile.txt', 'r'), file is

A

an identifier that refers to the file we are using (you could give it a different name). It is referred to as the file handler.

18
Q

file opening modes

A

There are different modes in which you can open files. The most common are read only mode (you cannot write to the file), write mode (you can write to the file, existing contents are deleted) and append mode (adds content to the end of a file without deleting any existing contents).

19
Q

file handling syntax

py

A
file = open(fileName, mode)     #open the file
file.read()                     #read all the file
file.readline()                 #read the next line
file.write(string)              #write string to the file
file.close()                    #close the file

Modes
“r” - read only, the file pointer is placed at the start of the file, ready for reading.
“w” - write, this will overwrite what is currently in a file. If the file doesn’t exist it will create a new one.
“a” - append, this will append (add) to the end of an existing file and not overwrite any contents.

20
Q

Files

summary

A

Computer programs store data that will be required again in a file.
Every file is identified by its filename.
For example, any data held in an array while your program is executing will be lost when the program stops. You can save the data out to a file and read it back in when your program requires it on subsequent executions.

21
Q

File handling

pseudocode

A

A text file consists of a sequence of characters formatted into lines. Each line is terminated by an end of-line marker. The text file is terminated by an end-of-file marker.
In pseudocode, text files are handled using the following statements.
To open a file before reading from it or writing to it:
~~~
OPEN <file> FOR <file> // open file for mode (reading, writing..)
~~~</file></file>

OPENFILE <filename> FOR WRITE // open the file forwriting

Once the file is opened in READ mode, it can be read from a line at a time:
READFILE <file identifier>, <variable> // read a line of text from the file

Once the file is opened in WRITE or APPEND mode, it can be written to a line
at a time:
WRITEFILE <file identifier>, <variable> // write a line of text to the file

When a file is no longer being used it should be closed:
CLOSEFILE <file identifier> // close file

22
Q

File EOF marker

pseudocode

A

If we want to read a file from beginning to end, we can use a conditional loop. Text files contain a special marker at the end of the file that we can test for. Testing for this special end-of-file marker is a standard function in many programming languages. Every time this function is called it will test for this marker. The function will return FALSE if the end of the file is not yet reached and will return TRUE if the end-of-file marker has been reached.
In pseudocode we call this function EOF(). We can use the construct REPEAT...UNTIL EOF(). If it is possible that the file contains no data, it is better to use the construct:WHILE NOT EOF().
EOF(<file identifier>)

23
Q

example

A

For example, the following pseudocode statements read a text file and output itscontents:
~~~
OPENFILE “Test.txt” FOR READ
WHILE NOT EOF(“Test.txt”) DO
READFILE “Test.txt”, TextString
OUTPUT TextString
ENDWHILE
CLOSEFILE “Test.txt”
~~~

24
Q

example

A

This pseudocode shows how the file myText.txt could be written to and read from:
~~~
DECLARE textLn : STRING
DECLARE myFile : STRING
myFile ← “myText.txt”
OPEN myFile FOR WRITE
REPEAT
OUTPUT “Please enter a line of text”
INPUT textLn
IF textLn <> “”
THEN
WRITEFILE, textLn
ELSE
CLOSEFILE(myFile)
ENDIF
UNTIL textLn = “”
OUTPUT “The file contains these lines of text:”
OPEN myFile FOR READ
REPEAT
READFILE, textLn
OUTPUT textLn
UNTIL EOF(myFile)
CLOSEFILE(myFile)
~~~