โ† All HACKERRANK Flashcard Decks

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
  1. What does the `enumerate()` function return?

    Answer: An iterator of (index, value) pairs

    `enumerate()` yields `(index, value)` tuples as you iterate over a sequence.

  2. 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.

  3. 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.

  4. 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].

  5. 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.

  6. 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.

  7. 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')`.