0
0
Pythonprogramming~10 mins

Flushing and buffering concepts in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to flush the output buffer immediately.

Python
print('Hello, world!', end='')
import sys
sys.stdout.[1]()
Drag options to blanks, or click blank then click option'
Awrite
Bflush
Cclose
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Using write() instead of flush()
Trying to use close() which stops output
Using read() which is for input
2fill in blank
medium

Complete the code to open a file with buffering disabled.

Python
with open('log.txt', 'w', buffering=[1]) as f:
    f.write('Log start\n')
Drag options to blanks, or click blank then click option'
ANone
B1
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using -1 which means default buffering
Using None which is invalid here
Using 1 which means line buffering
3fill in blank
hard

Fix the error in flushing the file output.

Python
f = open('data.txt', 'w')
f.write('Data')
f.[1]()
Drag options to blanks, or click blank then click option'
Aflush
Bflushes
Cflushd
Dflushed
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like flushes or flushed
Trying to call flush as an attribute
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 letters.

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'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of length
Using less than instead of greater than in condition
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.

Python
data = {'a': 1, 'b': 0, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys instead of uppercase
Using less than or equal instead of greater than
Using keys as values instead of values