0
0
Pythonprogramming~10 mins

Overwrite vs append behavior in Python - Interactive Practice

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

Complete the code to add a new item to the list without removing existing items.

Python
my_list = [1, 2, 3]
my_list.[1](4)
print(my_list)
Drag options to blanks, or click blank then click option'
Ainsert
Bextend
Cappend
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' adds multiple items and expects an iterable.
Using 'remove' deletes an item instead of adding.
2fill in blank
medium

Complete the code to replace the value of the key 'name' in the dictionary.

Python
person = {'name': 'Alice', 'age': 30}
person['name'] = [1]
print(person)
Drag options to blanks, or click blank then click option'
A'Bob'
B'Alice'
C'30'
D'age'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the old value again instead of the new one.
Forgetting quotes around the string.
3fill in blank
hard

Fix the error in the code to add a new key-value pair to the dictionary without overwriting existing keys.

Python
settings = {'volume': 5, 'brightness': 7}
settings.[1]({'contrast': 10})
print(settings)
Drag options to blanks, or click blank then click option'
Aappend
Bupdate
Cadd
Dextend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'append' which is not a dictionary method.
Using 'add' which does not exist for dictionaries.
4fill in blank
hard

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

Python
words = ['cat', 'house', 'dog', 'elephant']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length.
Using the wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values only if the value is greater than 0.

Python
data = {'a': 1, 'b': 0, 'c': 3}
result = [1]: [2] for k, v in data.items() if v [3] 0
print(result)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting keys to uppercase.
Using wrong comparison operator or filtering condition.