list(map(lambda x: x + 10, [1, 2, 3]))
[11, 12, 13]
Which function do you use to create a file object?
open
Suggest an example where the IndexError is triggered.
[1, 2, 3][5]
list(filter(lambda x: x % 2 == 1, [1, 2, 3, 4, 5]))
[1, 3, 5]
What does f.readlines() return? (f is a given file object)
Read all the lines from a file object f and returns them as a list of strings.
Suggest an example where the FileNotFoundError is triggered.
open("no_such_file.txt")
list(map(str, [1, 2, 3]))
['1', '2', '3']
What are the 3 basic file modes?
'r', 'w', 'a'
Why is it recommended to specify the error type when we try to use a try except statement?
Make your code safer, more predictable, and easier to debug. Without error type it will catch unintended exceptions.
list(filter(lambda k: len(k) > 3, {"cat": 1, "lion": 2, "dog": 3, "tiger": 4}))
['lion', 'tiger']
Describe a syntax that avoids explicitly calling f.close() where f is a file object.
with open("example.txt", "r") as f:
data = f.read()
Suggest an example where the NameError is triggered.
print(unknown_variable)
list(map(lambda x: x*2, filter(lambda x: x > 2, [1, 2, 3, 4])))
[6, 8]
How do you save and load Python objects?
import json
with open("data.json", "w") as f: # 'w' = write mode
json.dump(data, f)
with open("data.json", "r") as f: # 'r' = read mode
loaded_data = json.load(f)
Suggest examples for TypeError and ValueError.
TypeError: 3 + "three"
ValueError: int("abc")