Python Basics Flashcards
7 cards from real HACKERRANK 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 Basics flashcards as text
What does the `enumerate()` function return?
Answer: An iterator of (index, value) pairs
`enumerate()` yields `(index, value)` tuples as you iterate over a sequence.
What is the result of `sorted([3, 1, 4, 1, 5], reverse=True)`?
Answer: [5, 4, 3, 1, 1]
`sorted()` with `reverse=True` returns a new list sorted in descending order.
Which statement about Python's `is` operator is correct?
Answer: It checks identity (same object in memory)
`is` checks whether two variables point to the same object in memory, not just equal values.
What will `[x**2 for x in range(4) if x % 2 == 0]` produce?
Answer: [0, 4]
The filter keeps only even x (0 and 2), then squares them to give [0, 4].
Which method would you use to split a string `s` on whitespace into a list of words?
Answer: s.split()
`str.split()` with no argument splits on any whitespace and removes empty strings.
What is the difference between `append()` and `extend()` on a list?
Answer: `append()` adds one element; `extend()` adds each element of an iterable
`append()` inserts its argument as a single element; `extend()` iterates over the argument and adds each item.
In Python, which of the following correctly defines a function with a default parameter value?
Answer: def greet(name='World'):
Default parameter values are specified with `=` in the function signature: `def greet(name='World')`.