Complete the code to open a file named 'data.txt' for reading.
file = open('data.txt', '[1]')
The mode 'r' opens the file for reading only.
Complete the code to read the first line from the file object 'file'.
line = file.[1]()The method readline() reads one line from the file.
Fix the error in the code to properly close the file after reading.
file = open('data.txt', 'r') content = file.read() file.[1]()
The correct method to close a file is close(), called as file.close().
Fill both blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
Use len(word) to get the length and filter words with length greater than 3 using >.
Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 3 characters.
words = ['apple', 'bat', 'carrot', 'dog'] result = { [1]: [2] for word in words if len(word) [3] 3 }
Use word.upper() for keys, len(word) for values, and filter words with length greater than 3 using >.