0
0
Pythonprogramming~10 mins

List comprehension vs loop in Python - Interactive 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 using a loop.

Python
squares = []
for i in range(5):
    squares.append(i[1]2)
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 (+) or subtraction (-) by mistake.
2fill in blank
medium

Complete the list comprehension to create a list of squares.

Python
squares = [i[1]2 for i in range(5)]
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 (+) or subtraction (-) by mistake.
3fill in blank
hard

Fix the error in the loop that tries to create a list of even numbers.

Python
evens = []
for num in range(10):
    if num [1] 2 == 0:
        evens.append(num)
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 modulo (%).
Using floor division (//) or exponentiation (**) incorrectly.
4fill in blank
hard

Fill both blanks to create a list of even squares using list comprehension.

Python
even_squares = [x[1]2 for x in range(10) if x [2] 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 addition (+) or equality (==) in the wrong place.
Mixing up operators for squaring and checking evenness.
5fill in blank
hard

Fill all three blanks to create a dictionary of numbers and their squares for odd numbers only.

Python
odd_squares = [1]: [1] for [2] in range(10) if [3] % 2 != 0}
print(odd_squares)
Drag options to blanks, or click blank then click option'
Anum
Bnum**2
Di
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using different variable names inconsistently.
Not squaring the number for the value.