0
0
Pythonprogramming~10 mins

Basic list comprehension syntax 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 list of squares from 1 to 5.

Python
squares = [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 * instead of ** which multiplies but does not square.
Using + which adds instead of powers.
2fill in blank
medium

Complete the code to create a list of even numbers from 0 to 10.

Python
evens = [x for x in range(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 // which is floor division, not remainder.
Using ** which is power, not related to even check.
3fill in blank
hard

Fix the error in the list comprehension to get lengths of words.

Python
lengths = [[1] for word in words]
Drag options to blanks, or click blank then click option'
Alength(word)
Bword.len()
Cword.length
Dlen(word)
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to use word.len() which is not valid in Python.
Using length(word) which is not a Python function.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 letters.

Python
lengths = {word: [1] for word in words if [2] > 3}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Dword.length
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using word.length which is not valid in Python.
Using word instead of len(word) for length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values for words longer than 4 letters.

Python
result = [1]: [2] for w in words if [3] > 4}
Drag options to blanks, or click blank then click option'
Aw.upper()
Bw
Clen(w)
Dw.lower()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using w.lower() instead of uppercase for keys.
Using w.length which is invalid in Python.