Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a list that doubles each number in the range 1 to 5.
Python
result = [x[1]2 for x in range(1, 6)] print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' will add 2 instead of doubling.
Using '**' will square the number, not double it.
✗ Incorrect
The * operator multiplies each number by 2, doubling it.
2fill in blank
mediumComplete the code to create a list that labels numbers as 'even' or 'odd' using if–else inside a list comprehension.
Python
labels = ['even' if x[1] 2 == 0 else 'odd' for x in range(1, 6)] print(labels)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '**' will not check divisibility.
Using '//' gives the quotient, not the remainder.
✗ Incorrect
The '%' operator gives the remainder. If remainder is 0 when divided by 2, the number is even.
3fill in blank
hardFix the error in the list comprehension that should replace negative numbers with 0.
Python
numbers = [-3, 5, -1, 7] result = [x if x [1] 0 else 0 for x in numbers] print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will keep negative numbers instead of replacing them.
Using '==' or '!=' will not correctly filter positive numbers.
✗ Incorrect
We keep x if it is greater than 0; otherwise, replace with 0.
4fill in blank
hardFill both blanks to create a list that squares even numbers and cubes odd numbers from 1 to 5.
Python
result = [x[1]2 if x [2] 2 == 0 else x**3 for x in range(1, 6)] print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '**' will multiply but not square.
Using '+' instead of '%' will not check evenness.
✗ Incorrect
Use '**' to square numbers and '%' to check if a number is even.
5fill in blank
hardFill both blanks to create a list of strings labeling numbers as 'small', 'medium', or 'large' based on their value.
Python
sizes = ['small' if x [1] 3 else 'medium' if x [2] 6 else 'large' for x in range(1, 10)] print(sizes)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '<' changes the boundary conditions.
Using '>' or '>=' instead of '<' will misclassify numbers.
✗ Incorrect
Use '<' to check if x is less than 3 or 6.