Complete the code to create a dictionary with numbers as keys and their squares as values.
squares = {x: x[1]2 for x in range(1, 6)}
print(squares)The ** operator is used to raise a number to a power. Here, it squares the number.
Complete the code to create a dictionary of words and their lengths from a list.
words = ['apple', 'banana', 'cherry'] lengths = {word: [1] for word in words} print(lengths)
word.length which is not valid in Python.The len() function returns the length of a string in Python.
Fix the error in the dictionary comprehension to include only even numbers as keys.
evens = {x: x**2 for x in range(1, 11) if x [1] 2 == 0}
print(evens)!= instead of % for checking even numbers.The modulo operator % gives the remainder. If remainder is 0 when divided by 2, the number is even.
Fill both blanks to create a dictionary of numbers and their cubes only if the cube is greater than 10.
cubes = {x: x[1]3 for x in range(1, 6) if x[2]3 > 10}
print(cubes)Use ** to cube the number and > to check if the cube is greater than 10.
Fill all three blanks to create a dictionary with uppercase keys and values only if the length of the word is greater than 4.
words = ['dog', 'elephant', 'cat', 'giraffe'] result = [1]: [2] for word in words if len(word) [3] 4 print(result)
Use word.upper() to make keys uppercase, keep values as word, and filter words with length greater than 4 using >.