Filter & Map
File Handling
Type of Error
100

list(map(lambda x: x + 10, [1, 2, 3]))


[11, 12, 13]

100

Which function do you use to create a file object?

open

100

Suggest an example where the IndexError is triggered.

[1, 2, 3][5]

200

list(filter(lambda x: x % 2 == 1, [1, 2, 3, 4, 5]))


[1, 3, 5]

200

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.

200

Suggest an example where the FileNotFoundError is triggered.

open("no_such_file.txt")

300

list(map(str, [1, 2, 3]))


['1', '2', '3']

300

What are the 3 basic file modes?

'r', 'w', 'a'

300

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.

400

list(filter(lambda k: len(k) > 3, {"cat": 1, "lion": 2, "dog": 3, "tiger": 4}))


['lion', 'tiger']

400

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()

400

Suggest an example where the NameError is triggered.

print(unknown_variable)

500

list(map(lambda x: x*2, filter(lambda x: x > 2, [1, 2, 3, 4])))


[6, 8]

500

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)

500

Suggest examples for TypeError and ValueError.

TypeError: 3 + "three"

ValueError: int("abc")


M
e
n
u