Complete the code to safely get the value for key 'name' from the dictionary.
person = {'name': 'Alice', 'age': 30}
name = person.[1]('name')
print(name)The get method safely retrieves the value for the given key without causing an error if the key is missing.
Complete the code to get the value for 'city' with a default of 'Unknown'.
location = {'country': 'USA'}
city = location.[1]('city', 'Unknown')
print(city)The get method can take a second argument as a default value if the key is not found.
Fix the error by completing the code to safely access 'score' from the dictionary.
results = {'player': 'Bob'}
score = results.[1]('score')
print(score)Using get avoids KeyError when 'score' key is missing.
Fill both blanks to create a dictionary of word lengths only for words longer than 3 letters.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if [2] > 3}
Use len(word) to get length and filter words with length greater than 3.
Fill all three blanks to create a dictionary with uppercase keys and values only if value is positive.
data = {'a': 1, 'b': -2, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }Keys are converted to uppercase with k.upper(), values are v, and only positive values are included using >.