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, x**2 means x squared.
Complete the code to create a dictionary with words as keys and their lengths as values.
words = ['apple', 'banana', 'cherry'] lengths = {word: [1] for word in words} print(lengths)
word.upper() returns the uppercase word, not its length.The len() function returns the length of a string. Here, it gives the length of each word.
Fix the error in the dictionary comprehension to create a dictionary with numbers as keys and their cubes as values.
cubes = {n: n[1]3 for n in range(1, 5)}
print(cubes)The exponentiation operator ** is needed to cube the number (raise it to the power of 3).
Complete the code to create a dictionary with words as keys and their first letters as values, but only for words longer than 5 letters.
words = 'apple', 'banana', 'cherry', 'date'] result = {word: word[0 for word in words if len(word) [1] 5} print(result)
len(word) > 5 filters words longer than 5 letters.Fill both blanks to create a dictionary with uppercase words as keys and their lengths as values, but only for words with length less than 6.
words = ['apple', 'banana', 'cherry', 'date'] result = {word.upper(): {BLANK_2}} for word in words if len(word) {{BLANK_2}} 6 print(result)
The dictionary comprehension starts with {. The keys are uppercase words using word.upper() (inside braces), the values are lengths with len(word), and the condition filters words with length less than 6 using <.