Last Updated: December 6, 2025
31 quizzes
Use a style-checking tool to verify that the current file follows PEP 8 naming and spacing rules.
subprocess.run(["python", "-m", "flake8", ])Click an option to fill the blank:
Format the value of total_requests into a readable string using an f-string before logging it.
logger.info(f"Total requests: {}")Click an option to fill the blank:
Retrieve a configuration value with a safe default so the code remains robust in production.
timeout = config.get("REQUEST_TIMEOUT", )Click an option to fill the blank:
According to PEP 8, which function name is preferred for readability?
Which is the most PEP 8 compliant way to add spaces around an assignment?
Which tool is commonly used to automatically format Python code according to a consistent style?
Which file is typically used to provide a high-level overview and usage instructions for a Python project?
Where should a function’s docstring be placed to follow best practices?
Which of the following is the most Pythonic way to iterate over items in a list with their index?
Which design pattern best describes a class that provides a single shared database connection instance?
A class that handles logging, database access, and HTTP requests all together is an example of which anti-pattern?
Which data structure is usually best for fast membership checks in large collections?
Which practice helps protect a Python application from known vulnerabilities in third-party packages?
Which line keeps code within the recommended 79-character limit when building a long string?
Order the steps to add documentation to a new Python module and make it discoverable.
Drag and drop to reorder, or use the arrows.
Order the steps to safely optimize a slow function in a production codebase.
Drag and drop to reorder, or use the arrows.
What is the output of this code that uses a Pythonic list comprehension?
1numbers = [1, 2, 3, 4]
2result = [n * 2 for n in numbers if n % 2 == 0]
3print(result)What is the output when accessing a dictionary using the get method with a default?
1config = {"timeout": 10}
2value = config.get("retries", 3)
3print(value)What is the output of this code using a small Singleton-like cache?
1class Settings:
2 _instance = None
3 def __new__(cls):
4 if cls._instance is None:
5 cls._instance = super().__new__(cls)
6 return cls._instance
7
8first = Settings()
9second = Settings()
10print(first is second)What is the output when using a set for membership testing?
1allowed_roles = {"admin", "editor", "viewer"}
2print("guest" in allowed_roles)What is the output of this code that uses a docstring and prints the function’s __doc__?
1def greet(name):
2 """Return a polite greeting."""
3 return f"Hello, {name}!"
4
5print(greet.__doc__)Find the bug in this code that is meant to validate user input and log an error.
Click on the line(s) that contain the bug.
import logging logger = logging.getLogger(__name__) def get_age(age): if not isinstance(age, int) or age < 0: logger.error("Invalid age: %s", age) raise ValueError("Age must be non-negative integer") return age Find the bug in this code that attempts to cache configuration in a Singleton-style class.
Click on the line(s) that contain the bug.
class Config: _instance = None def __new__(cls, settings=None): if cls._instance is None: cls._instance = super().__new__(cls) cls.settings = settings or {} return cls._instance config1 = Config({"debug": True})config2 = Config({"debug": False}) Match each design pattern to its primary intent.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match each security practice with its description.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the function to document and compute an average response time using Pythonic tools.
def average_response_time(durations): """""" if not durations: return total = sum(durations) return total / Click an option to fill blank 1:
Complete this secure function that reads a setting with a safe default and validates it.
def get_max_items(config): """Return a validated maximum number of items to process.""" raw_value = config.get("MAX_ITEMS", ) if not isinstance(raw_value, int) or raw_value <= 0: raise ValueError("MAX_ITEMS must be a positive integer") return min(raw_value, )Click an option to fill blank 1:
Complete this Pythonic function that returns a sorted list of unique user IDs.
def normalize_user_ids(user_ids): """Return unique, sorted user IDs as strings.""" unique_ids = (str(uid) for uid in user_ids) return (unique_ids)Click an option to fill blank 1:
Complete this context manager-based function to measure execution time of a block.
import timefrom contextlib import contextmanager@contextmanagerdef measure(label): start = time.() try: yield finally: duration = time.() - start print(f"{label} took {duration:.4f} ")Click an option to fill blank 1:
Click the line that violates PEP 8 naming conventions for a constant.
Click on the line to select.
MAX_CONNECTIONS = 10pi_value = 3.14159DbHost = "db.internal"DEFAULT_TIMEOUT = 5 Click the line that represents the God Object anti-pattern by taking on too many responsibilities.
Click on the line to select.
class UserService: def create_user(self, data): self._save_to_db(data) self._send_welcome_email(data) self._rebuild_search_index()