Complete the code to create a lambda function that adds 5 to a number.
add_five = lambda x: x [1] 5 print(add_five(10)) # Output: 15
The lambda function adds 5 to the input number using the + operator.
Complete the code to use a lambda function with the map() function to square each number in the list.
numbers = [1, 2, 3, 4] squared = list(map(lambda x: x [1] x, numbers)) print(squared) # Output: [1, 4, 9, 16]
The lambda function multiplies the number by itself to get the square.
Fix the error in the lambda function that filters even numbers using filter().
numbers = [1, 2, 3, 4, 5, 6] even_numbers = list(filter(lambda x: x [1] 2 == 0, numbers)) print(even_numbers) # Output: [2, 4, 6]
The modulo operator '%' returns the remainder. Even numbers have remainder 0 when divided by 2.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
words = ['apple', 'bat', 'car', 'door'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths) # Output: {'apple': 5, 'door': 4}
The dictionary comprehension uses len(word) to get length and filters words with length greater than 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is less than 5.
words = ['tree', 'sun', 'flower', 'sky'] result = { [1]: [2] for w in words if len(w) [3] 5 } print(result) # Output: {'TREE': 4, 'SUN': 3, 'SKY': 3}
The comprehension maps uppercase words to their lengths, filtering words shorter than 5 characters.