Complete the code to open a file using a context manager.
with open('example.txt', [1]) as file: content = file.read()
The mode 'r' opens the file for reading, which is the default and correct here.
Complete the code to automatically close the file after reading.
with open('data.txt', 'r') as [1]: data = file.read()
The variable name after 'as' is used to refer to the file object inside the block.
Fix the error in the code to ensure the file is closed automatically.
file = open('log.txt', 'r') try: content = [1].read() finally: file.close()
The variable 'file' holds the file object and must be used to call read().
Fill both blanks to create a dictionary with word lengths for words longer than 3 characters.
lengths = {word: [1] for word in words if len(word) [2] 3}We use len(word) to get the length and '>' to filter words longer than 3 characters.
Fill all three blanks to create a dictionary with uppercase keys and values greater than zero.
result = [1]: [2] for k, v in data.items() if v [3] 0}
Keys are converted to uppercase with k.upper(), values are v, and we filter values greater than zero using '>'.