Complete the code to open a file named 'data.txt' for reading.
file = open('data.txt', '[1]')
To read a file, you open it with mode 'r'.
Complete the code to write the string 'Hello' to a file named 'output.txt'.
with open('output.txt', '[1]') as f: f.write('Hello')
Use mode 'w' to write to a file, which creates or overwrites it.
Fix the error in the code to read all lines from 'log.txt'.
with open('log.txt', 'r') as f: lines = f.[1]()
The method 'readlines()' reads all lines into a list.
Fill both blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 letters.
words = ['apple', 'cat', 'banana', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
Use len(word) to get length and '>' to filter words longer than 3 letters.
Fill all three blanks to create a dictionary with uppercase words as keys, their lengths as values, only for words longer than 4 letters.
words = ['tree', 'house', 'car', 'elephant'] result = { [1]: [2] for w in words if len(w) [3] 4 }
Use w.upper() for keys, len(w) for values, and '>' to filter words longer than 4 letters.