Last Updated: January 3, 2026
31 quizzes
Complete the code so the sum of all values in prices is displayed to the user.
print(sum(prices) 2)Click an option to fill the blank:
Complete the code so the function full_name is passed as a callback to sorted for custom sorting.
sorted(users, key=)Click an option to fill the blank:
Complete the code so the lambda doubles every element in data using map.
result = list(map(, data))Click an option to fill the blank:
Which statement best describes a Python function?
What happens if a Python function has no return statement?
Given def area(length, width): return length * width, what are length and width called?
Which call correctly uses keyword arguments for def connect(host, port, secure=False)?
What is the main benefit of using *args in a function definition?
Which statement about **kwargs is true?
What is a key limitation of lambda functions compared to def-defined functions?
In Python's LEGB rule, which scope is searched first when looking up a variable name?
Which keyword allows a nested function to reassign a variable from its enclosing (non-global) scope?
What is the main purpose of a decorator in Python?
Which functools tool is most suitable for caching expensive function results?
Order the steps to create and use a recursive function that sums a list of numbers.
Drag and drop to reorder, or use the arrows.
Order the steps to build a closure that counts how many times it has been called.
Drag and drop to reorder, or use the arrows.
What is the output of this code using default arguments?
1def greet(user='Guest'):
2 return f'Hello, {user}!'
3
4message = greet()
5print(message)What is the output of this code involving *args?
1def multiply_all(*values):
2 result = 1
3 for number in values:
4 result *= number
5 return result
6
7print(multiply_all(2, 3, 4))What is the output of this recursion example?
1def countdown(n):
2 if n == 0:
3 return 'Go!'
4 return countdown(n - 1)
5
6print(countdown(3))What is the output of this closure example?
1def make_adder(x):
2 def adder(y):
3 return x + y
4 return adder
5
6add_ten = make_adder(10)
7print(add_ten(3))What will this decorator example print?
1def trace(func):
2 def wrapper(*args, **kwargs):
3 result = func(*args, **kwargs)
4 return result
5 return wrapper
6
7@trace
8def square(value):
9 return value * value
10
11print(square(4))This function should use a global counter to track how many times it was called. Find the bug.
Click on the line(s) that contain the bug.
count_calls = 0 def log_call(): count_calls += 1 return count_callsThis code tries to build a list of callbacks that remember their index. Find the bug.
Click on the line(s) that contain the bug.
def make_callbacks(): callbacks = [] for index in range(3): def cb(): return index callbacks.append(cb) return callbacksMatch each recursion term with its role.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match each functools tool with its primary use.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the code to build a decorator that times how long a function takes and prints the duration.
import timedef timed(action): def wrapper(*values, **options): start = () result = action(*values, **options) end = time.time() print('Elapsed:', - start) return result return wrapperClick an option to fill blank 1:
Complete the code so the factory function returns a closure that applies a percentage discount.
def make_discount(rate): def apply(price): return price * (1 - ) return Click an option to fill blank 1:
Complete the code so this recursive function computes the nth Fibonacci number with caching.
from functools import lru_cache@lru_cache(maxsize=None)def fib(position): if position < 2: return position return fib(position - 1) + Click an option to fill blank 1:
Complete the code so the function describe_user accepts any keyword details and shows how many were provided.
def describe_user(username, **details): info = f'User: {username}' count = len() return info + f' (fields: {count})'Click an option to fill blank 1:
Click the line that uses a global variable inside the function.
Click on the line to select.
total_requests = 0 def record_request(): global total_requests total_requests += 1 return total_requestsClick the line where a nested function accesses a variable from its enclosing scope.
Click on the line to select.
def outer_task(): task_name = 'backup' def inner_task(): return f'Running {task_name}' return inner_task()