Data Science with Python Certification Introduction to Python for Data Science 2 — Questions and Answers
Question 1: Which Python data type is immutable and can be used as a dictionary key?
- list
- tuple (Correct answer)
- set
- dict
Correct answer: tuple
Tuples are immutable sequences and can be used as dictionary keys, unlike lists or sets.
Question 2: What does the `zip()` function return when passed two lists of unequal length?
- Raises a ValueError
- Pads the shorter list with None
- Stops at the length of the shorter list (Correct answer)
- Stops at the length of the longer list
Correct answer: Stops at the length of the shorter list
`zip()` stops producing tuples when the shortest input iterable is exhausted.
Question 3: Which method removes and returns the last element of a Python list?
- list.remove()
- list.delete()
- list.pop() (Correct answer)
- list.discard()
Correct answer: list.pop()
`list.pop()` removes and returns the last element by default, or an element at a given index.
Question 4: What is the result of `bool([])` in Python?
- True
- False (Correct answer)
- None
- Raises TypeError
Correct answer: False
An empty list is falsy in Python, so `bool([])` evaluates to `False`.
Question 5: Which of the following correctly creates a dictionary using a dictionary comprehension?
- {x: x**2 for x in range(5)} (Correct answer)
- [x: x**2 for x in range(5)]
- {x, x**2 for x in range(5)}
- (x: x**2 for x in range(5))
Correct answer: {x: x**2 for x in range(5)}
Dictionary comprehensions use curly braces with a `key: value` expression followed by a `for` clause.
Question 6: What will `'data'[::-1]` return in Python?
- 'data'
- 'atad' (Correct answer)
- 'dta'
- Raises IndexError
Correct answer: 'atad'
The slice `[::-1]` reverses a string by stepping backwards through all characters.
Question 7: In Python, which keyword is used to define an anonymous function?
- def
- func
- lambda (Correct answer)
- anon
Correct answer: lambda
`lambda` creates small anonymous functions that can have any number of arguments but only one expression.
Which Python data type is immutable and can be used as a dictionary key?