Complete the code to check if any number in the list is greater than 5 using the any() function.
numbers = [1, 3, 7, 2] result = any(num [1] 5 for num in numbers) print(result)
The any() function returns True if at least one element in the iterable is True. Here, we check if any number is greater than 5.
Complete the code to check if all numbers in the list are positive using the all() function.
numbers = [4, 6, 8, 1] result = all(num [1] 0 for num in numbers) print(result)
The all() function returns True only if every element in the iterable is True. Here, we check if all numbers are greater than 0 (positive).
Fix the error in the code to correctly check if any word in the list starts with the letter 'a'.
words = ['apple', 'banana', 'cherry'] result = any(word.[1]('a') for word in words) print(result)
The correct method to check if a string starts with a specific character is startswith(). Other options are incorrect method names.
Fill both blanks to create a dictionary of words and their lengths, but only include words longer than 3 characters.
words = ['cat', 'elephant', 'dog', 'lion'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
We use len(word) to get the length of each word. The condition len(word) > 3 filters words longer than 3 characters.
Fill both blanks to create a dictionary with uppercase words as keys and their lengths as values, including only words with length greater than 4.
words = ['tree', 'mountain', 'river', 'sky'] result = {word.[1](): [2] for word in words if len(word) > 4} print(result)
upper() method with parentheses.The dictionary is created using curly braces {}. We convert words to uppercase with upper() and get their lengths with len(word). The condition filters words longer than 4 characters.