Python Flashcards
7 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Python flashcards as text
What does the `global` keyword do inside a Python function?
Answer: Declares that a variable name refers to the module-level global variable
`global x` inside a function tells Python to use and potentially modify the `x` defined at the module level rather than creating a local one.
Which built-in function applies a function to every item in an iterable and returns a map object?
Answer: map()
`map(func, iterable)` applies `func` to each element and returns an iterator of the results.
What is the output of `type(3.0)` in Python?
Answer:
`3.0` is a floating-point literal, so `type(3.0)` returns ``.
What does `'%s has %d items' % ('cart', 5)` produce?
Answer: 'cart has 5 items'
The `%` formatting operator substitutes `%s` with the string and `%d` with the integer from the tuple.
Which Python data structure guarantees FIFO (first-in, first-out) ordering?
Answer: collections.deque used as a queue
`collections.deque` with `append()` and `popleft()` provides O(1) FIFO queue operations.
What is the purpose of `if __name__ == '__main__':` in a Python script?
Answer: Both B and C
This guard ensures the indented block runs only when the script is executed directly, not when imported, effectively marking the entry point.
What does `sorted()` return compared to `list.sort()`?
Answer: `sorted()` returns a new list; `list.sort()` sorts in place and returns None
`sorted()` always returns a new sorted list, while `list.sort()` modifies the list in place and returns `None`.