0
0
Pythonprogramming~10 mins

Why dictionary comprehension is used in Python - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a dictionary with numbers as keys and their squares as values.

Python
squares = {x: x[1]2 for x in range(1, 6)}
print(squares)
Drag options to blanks, or click blank then click option'
A+
B**
C*
D//
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using multiplication operator instead of exponentiation.
2fill in blank
medium

Complete the code to create a dictionary of words and their lengths from a list.

Python
words = ['apple', 'banana', 'cherry']
lengths = {word: [1] for word in words}
print(lengths)
Drag options to blanks, or click blank then click option'
Aword
Blength(word)
Cword.length
Dlen(word)
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using incorrect syntax like word.length which is not valid in Python.
3fill in blank
hard

Fix the error in the dictionary comprehension to include only even numbers as keys.

Python
evens = {x: x**2 for x in range(1, 11) if x [1] 2 == 0}
print(evens)
Drag options to blanks, or click blank then click option'
A%
B!=
C//
D+
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using != instead of % for checking even numbers.
4fill in blank
hard

Fill both blanks to create a dictionary of numbers and their cubes only if the cube is greater than 10.

Python
cubes = {x: x[1]3 for x in range(1, 6) if x[2]3 > 10}
print(cubes)
Drag options to blanks, or click blank then click option'
A**
B*
C>
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using multiplication instead of exponentiation.
Using wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values only if the length of the word is greater than 4.

Python
words = ['dog', 'elephant', 'cat', 'giraffe']
result = [1]: [2] for word in words if len(word) [3] 4
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Bword
C>
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using wrong comparison operator.
Not converting keys to uppercase.