Complete the code to catch a division by zero error.
try: result = 10 [1] 0 except ZeroDivisionError: print("Cannot divide by zero")
The division operator / causes a ZeroDivisionError when dividing by zero.
Complete the code to convert a string to an integer safely.
try: number = int([1]) except ValueError: print("Invalid number")
Trying to convert a non-numeric string like "abc" to int raises a ValueError.
Fix the error in the code that causes a KeyError.
data = {'name': 'Alice'}
print(data[[1]])The key 'name' exists in the dictionary, so accessing it does not cause a KeyError.
Fill both blanks to create a dictionary comprehension that includes only words longer than 3 letters.
words = ['cat', 'lion', 'dog', 'elephant'] lengths = {word: [1] for word in words if len(word) [2] 3}
The comprehension maps each word to its length only if the word length is greater than 3.
Fill all three blanks to create a dictionary comprehension that includes uppercase keys for words with length greater than 4.
words = ['apple', 'bat', 'carrot', 'dog'] result = { [1]: [2] for word in words if len(word) [3] 4 }
The comprehension creates keys as uppercase words and values as the original words for those longer than 4 letters.