Complete the code to print each fruit in the list.
fruits = ['apple', 'banana', 'cherry'] for [1] in fruits: print([1])
We use a variable name to represent each element in the list during the loop. Here, fruit is the variable that takes each value from fruits.
Complete the code to sum all numbers in the list.
numbers = [1, 2, 3, 4] total = 0 for num in numbers: total += [1] print(total)
Inside the loop, num represents each number from the list. We add it to total to get the sum.
Fix the error in the loop to print each character in the word.
word = 'hello' for [1] in word: print(char)
The loop variable should be char because the print statement uses char. This way, each character is printed.
Fill both blanks to create a dictionary with words as keys and their lengths as values for words longer than 3 letters.
words = ['cat', 'house', 'tree', 'a', 'dog'] lengths = { [1]: [2]([1]) for [1] in words if len([1]) > 3 }
We use word as the variable for each item in the list. The function len() gives the length of each word.
Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values for words longer than 2 letters.
words = ['hi', 'hello', 'yes', 'no'] result = { [1]: [2] for [3] in words if len([3]) > 2 }
word is the loop variable. We use word.upper() as the key and len(word) as the value in the dictionary.