Complete the code to create a list of squares using a loop.
squares = [] for i in range(5): squares.append(i[1]2) print(squares)
The ** operator is used to raise a number to a power. Here, i**2 means i squared.
Complete the list comprehension to create a list of squares.
squares = [i[1]2 for i in range(5)] print(squares)
In list comprehensions, you can use the same operators as in regular code. Here, i**2 squares each number.
Fix the error in the loop that tries to create a list of even numbers.
evens = [] for num in range(10): if num [1] 2 == 0: evens.append(num) print(evens)
The modulo operator % gives the remainder of division. num % 2 == 0 checks if num is even.
Fill both blanks to create a list of even squares using list comprehension.
even_squares = [x[1]2 for x in range(10) if x [2] 2 == 0] print(even_squares)
The first blank uses ** to square the number. The second blank uses % to check if the number is even.
Fill all three blanks to create a dictionary of numbers and their squares for odd numbers only.
odd_squares = [1]: [1] for [2] in range(10) if [3] % 2 != 0} print(odd_squares)
The dictionary comprehension uses num as the key and num**2 as the value. The variable num iterates over the range.