Complete the code to define a serverless function that returns a greeting message.
def handler(event, context): return [1]
The serverless function should return a string as the output. Using return "Hello from serverless!" inside the function is correct, but since the code already has return, the blank should be just the string. So option D is correct.
Complete the code to extract the 'name' value from the event dictionary in a serverless function.
def handler(event, context): name = event[1] return f"Hello, {name}!"
In Python, to get a value from a dictionary, you use square brackets with the key as a string. So event["name"] is correct.
Fix the error in the serverless function code to correctly check if the event has a 'user' key.
def handler(event, context): if [1] in event: return "User found" else: return "User not found"
When checking if a key exists in a dictionary, the key must be a string in quotes. Both single and double quotes work, but since the code uses double quotes outside, single quotes inside avoid confusion. So 'user' is correct.
Fill both blanks to create a serverless function that returns the length of a list passed in the event under 'items'.
def handler(event, context): items = event[1] count = [2](items) return count
The function first extracts the list from the event dictionary using event["items"]. Then it uses the len() function to get the number of items in the list.
Fill all three blanks to create a serverless function that filters even numbers from a list in the event under 'numbers' and returns the filtered list.
def handler(event, context): numbers = event[1] evens = [num for num in numbers if num [2] 2 == 0] return [3]
The function gets the list from the event using event["numbers"]. It then uses a list comprehension to keep numbers where num % 2 == 0 (even numbers). Finally, it returns the filtered list stored in evens.