Complete the code to create a list of squares of numbers from 1 to 5.
squares = [x[1]2 for x in range(1, 6)] print(squares)
The ** operator is used to raise a number to a power. Here, x**2 means x squared.
Complete the code to create a list of even numbers from 1 to 10 using list comprehension with a condition.
evens = [x for x in range(1, 11) if x [1] 2 == 0] print(evens)
The % operator gives the remainder of division. If x % 2 == 0, x is even.
Fix the error in the list comprehension to get numbers greater than 5 from the list.
numbers = [1, 3, 6, 8, 2] greater_than_five = [n for n in numbers if n [1] 5] print(greater_than_five)
To get numbers greater than 5, use the greater than operator >.
Fill both blanks to create a list of squares of odd numbers from 1 to 10.
odd_squares = [x[1]2 for x in range(1, 11) if x [2] 2 != 0] print(odd_squares)
Use ** to square the number and % to check if the number is odd by checking remainder not equal to zero.
Fill all three blanks to create a list of uppercase words that have length greater than 3.
words = ['apple', 'bat', 'carrot', 'dog'] result = [[1].upper() for [2] in words if len([3]) > 3] print(result)
.upper() to the list instead of the word.Use the variable word consistently to refer to each item in the list. Apply .upper() to convert to uppercase and check length with len(word) > 3.