Complete the code to create a list of squares for numbers 1 to 5.
squares = [x[1]2 for x in range(1, 6)] print(squares)
Using ** raises x to the power of 2, which calculates squares.
Complete the code to filter numbers greater than 10 from the list.
numbers = [5, 12, 7, 18, 3] filtered = [n for n in numbers if n [1] 10] print(filtered)
The operator > selects numbers greater than 10.
Fix the error in the code to calculate the average of a list.
data = [10, 20, 30] avg = sum(data) [1] len(data) print(avg)
To find the average, divide the sum by the number of items using /.
Fill both blanks to create a dictionary of word lengths for words longer than 3 letters.
words = ['data', 'is', 'fun', 'and', 'big'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
The dictionary maps each word to its length using len(word). The condition > selects words longer than 3 letters.
Fill all three blanks to create a dictionary with uppercase keys and values greater than 0.
data = {'a': 1, 'b': -2, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
print(result)The dictionary comprehension uses k.upper() for uppercase keys, v for values, and filters values greater than 0 with >.