Complete the code to create a list of squares from 1 to 5 using list comprehension.
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 0 to 10 using list comprehension.
evens = [x for x in range(11) if x [1] 2 == 0] print(evens)
The % operator gives the remainder. If x % 2 == 0, x is even.
Fix the error in the list comprehension to create a list of uppercase words.
words = ['apple', 'banana', 'cherry'] upper_words = [word[1] for word in words] print(upper_words)
The .upper() method converts a string to uppercase letters.
Fill both blanks to create a list of squares for numbers greater than 3.
result = [x[1]2 for x in range(1, 7) if x [2] 3] print(result)
Use ** to square numbers and > to filter numbers greater than 3.
Fill all three blanks to create a dictionary of word lengths for words longer than 4 letters.
word_lengths = [1]: [1] for [2] in words if len([3]) > 4}
The dictionary keys are words (word), values are their lengths (len(word)), and the loop variable is word.