Extra: Python Coding Flashcards

1
Q

What are the different file access functions in python?

A

Read -> f = open(“FileName.txt”, “r”)
Write -> f = open(“FileName.txt”, “w”)
Append -> f = open(“FileName.txt”, “a”)
Create -> f = open(“FileName.txt”, “x”)

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

What is the code to read a file? (or by first line)

A

f = open(“FileName.txt”, “r”)
text = f.read() // text = f.readline()
f.close()

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

How do you read a file line by line?

A

f = open(“FileName.txt”, “r”)
for x in f:
print(x)

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

How do you write onto a file and what does it do?

A

f = open(“FileText.txt”, “w”)
f.write(“newText”)
f.close()

This deletes the current contents stored on the file and give it new contents

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

How do you append a file and what does it do?

A

f = open(“FileText.txt”, “a”)
f.write(“newText”)
f.close()

Give the file new contents that will appear at the end of the file. If the file doesn’t exist, it will create the file

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

How do you run a statement for each content in a 2D array?

A

array = [“John”, “IceCream”, “Burgers”], [“Sahra”, “Burgers”, “Chilli”], [“Michal”, “Chips”, “Curry”], [“Janet”, “Popcorn”, “Doritos”]
for row in array:
for item in row:
statement(s)

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

How could you find a particular item in a 2D array?

A

array = [“John”, “IceCream”, “Burgers”], [“Sahra”, “Burgers”, “Chilli”], [“Michal”, “Chips”, “Curry”], [“Janet”, “Popcorn”, “Doritos”]

print(array[1][2])

“Chilli”

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