Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to open a large file for reading.
Python
with open('large_file.txt', [1]) as file: pass
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' or 'a' when you want to read the file.
✗ Incorrect
To read a file, you open it with mode 'r'.
2fill in blank
mediumComplete the code to read a large file line by line efficiently.
Python
with open('large_file.txt', 'r') as file: for [1] in file: print(line.strip())
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'char' or 'word' which do not represent lines.
✗ Incorrect
Each iteration reads one line, so 'line' is a good variable name.
3fill in blank
hardFix the error in the code to read a large file in chunks.
Python
with open('large_file.txt', 'r') as file: while True: chunk = file.read([1]) if not chunk: break print(chunk)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of an integer to read().
✗ Incorrect
The read() method expects an integer for the number of characters to read.
4fill in blank
hardFill both blanks to create a dictionary with word lengths for words longer than 3 characters.
Python
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '==' which filter wrong words.
✗ Incorrect
We want the length of each word and only words longer than 3 characters.
5fill in blank
hardFill all three blanks to create a dictionary with uppercase keys and the original words as values for words longer than 4 characters.
Python
words = ['apple', 'bat', 'carrot', 'dog'] result = { [1]: [2] for word in words if len(word) [3] 4 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(word) as value or wrong comparison operators.
✗ Incorrect
Keys are uppercase words, values are original words, filtered by length > 4.