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
Which keyword is used to define an anonymous (lambda) function in Python?
Answer: lambda
The `lambda` keyword creates small anonymous functions without a full `def` statement.
What does `range(2, 10, 3)` produce?
Answer: [2, 5, 8]
`range(2, 10, 3)` starts at 2, steps by 3, and stops before 10: 2, 5, 8.
Which of the following is a valid dictionary comprehension?
Answer: {k: v for k, v in items}
Dictionary comprehensions use curly braces with a `key: value` expression followed by `for`.
What happens when you access a dictionary key that does not exist using `dict['key']`?
Answer: Raises KeyError
Accessing a missing key with bracket notation raises a `KeyError` exception.
Which method removes and returns the last element of a list?
Answer: pop()
`list.pop()` without arguments removes and returns the last element of the list.
What is the correct way to open a file for writing in Python?
Answer: open('file.txt', 'w')
Mode `'w'` opens a file for writing, creating it if it doesn't exist and truncating it if it does.
What does the `*args` syntax in a function definition allow?
Answer: Any number of positional arguments
`*args` collects any number of extra positional arguments into a tuple inside the function.