0
0
Pythonprogramming~10 mins

List 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 list of squares of numbers 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 multiplication (*) instead of exponentiation (**).
Using addition (+) instead of exponentiation.
2fill in blank
medium

Complete the code to create a list of even numbers from 1 to 10 using list comprehension with a condition.

Python
evens = [x 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 addition (+) instead of modulus (%).
Using floor division (//) instead of modulus.
3fill in blank
hard

Fix the error in the list comprehension to get numbers greater than 5 from the list.

Python
numbers = [1, 3, 6, 8, 2]
greater_than_five = [n for n in numbers if n [1] 5]
print(greater_than_five)
Drag options to blanks, or click blank then click option'
A>
B<=
C==
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using less than (<) instead of greater than (>).
Using equality (==) instead of comparison.
4fill in blank
hard

Fill both blanks to create a list of squares of odd numbers from 1 to 10.

Python
odd_squares = [x[1]2 for x in range(1, 11) if x [2] 2 != 0]
print(odd_squares)
Drag options to blanks, or click blank then click option'
A**
B+
C%
D//
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using addition (+) instead of exponentiation.
Using floor division (//) instead of modulus.
5fill in blank
hard

Fill all three blanks to create a list of uppercase words that have length greater than 3.

Python
words = ['apple', 'bat', 'carrot', 'dog']
result = [[1].upper() for [2] in words if len([3]) > 3]
print(result)
Drag options to blanks, or click blank then click option'
Aword
Dwords
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using different variable names inconsistently.
Applying .upper() to the list instead of the word.