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 of the following creates a set in Python?
Answer: set()
`{}` creates an empty dict, so `set()` is the correct way to create an empty set.
What is the output of `list(map(lambda x: x*2, [1, 2, 3]))`?
Answer: [2, 4, 6]
`map` applies the lambda to each element, doubling 1, 2, 3 to produce [2, 4, 6].
Which exception is raised when dividing by zero in Python?
Answer: ZeroDivisionError
Python raises `ZeroDivisionError` specifically when the divisor is zero.
What does `'abc' * 3` produce in Python?
Answer: 'abcabcabc'
String repetition with `*` concatenates the string the given number of times.
Which of the following correctly checks if `x` is NOT in a list `lst`?
Answer: Both A and B
Both `x not in lst` and `not x in lst` are valid; `not in` is the idiomatic form.
What is the purpose of `__init__` in a Python class?
Answer: To initialize object attributes when an instance is created
`__init__` is the constructor method called automatically when a new object is instantiated.
Which of these is an immutable data type in Python?
Answer: tuple
Tuples are immutable; their elements cannot be changed after creation.