0
0
Pythonprogramming~10 mins

Dictionary comprehension with condition in Python - Interactive Code Practice

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 (*) instead of exponentiation (**).
Using addition (+) which adds numbers instead of squaring.
2fill in blank
medium

Complete the code to create a dictionary of numbers and their squares only for numbers greater than 3.

Python
filtered_squares = {x: x**2 for x in range(1, 7) if x [1] 3}
print(filtered_squares)
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using less than (<) which selects numbers smaller than 3.
Using equality (==) which selects only number 3.
3fill in blank
hard

Fix the error in the dictionary comprehension to include only even numbers and their squares.

Python
even_squares = {n: n**2 for n in range(1, 10) if n [1] 2 == 0}
print(even_squares)
Drag options to blanks, or click blank then click option'
A//
B%
C*
D**
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using floor division (//) which does not check for evenness.
Using multiplication (*) or exponentiation (**) which are unrelated.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths only if the length is greater than 4.

Python
word_lengths = {word: [1] for word in words if len(word) [2] 4}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the word itself as value instead of its length.
Using less than or equal (<=) which selects shorter words.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 3 letters.

Python
result = { [1]: [2] for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the original word as key instead of uppercase.
Using less than or equal (<=) which includes shorter words.