Prerequisite Review & refresh
🐍 Python Foundations & Advanced Features
PyTorch is a Python library, and idiomatic Python makes deep-learning code short and readable. Beyond the basics, a handful of features appear constantly in PyTorch and data code; know them and the framework stops feeling mysterious.
Multiple-choice questions on the topic itself. Pick an answer, then reveal it. If several are unclear, work through the review above first.
1. The list comprehension [x*x for x in range(4)] produces:
- [0, 1, 2, 3]
- [1, 4, 9, 16]
- [0, 1, 4, 9]
- [0, 2, 4, 6]
Show answer
Correct: C. range(4) is 0,1,2,3; their squares are 0,1,4,9.
2. A generator function is one that uses:
- return
- yield
- lambda
- async
Show answer
Correct: B. yield turns a function into a generator that produces values lazily, one at a time.
3. The main advantage of a generator over a list is:
- faster indexing
- lazy, memory-efficient iteration
- automatic sorting
- built-in JSON support
Show answer
Correct: B. A generator produces items on demand without holding the whole sequence in memory.
4. Which dunder methods let an object support len(obj) and obj[i]?
- __init__ and __str__
- __len__ and __getitem__
- __call__ and __repr__
- __add__ and __eq__
Show answer
Correct: B. __len__ backs len(); __getitem__ backs indexing and iteration (exactly what a Dataset implements).
5. Defining __call__ on a class allows:
- printing the object
- calling the instance like a function: obj(x)
- adding two objects
- iterating it
Show answer
Correct: B. __call__ makes instances callable; e.g. model(x) runs the object's logic.
6. zip([1, 2, 3], ['a', 'b', 'c']) yields:
- (1, 2, 3, 'a', 'b', 'c')
- (1, 'a'), (2, 'b'), (3, 'c')
- a dictionary
- [1, 'a', 2, 'b']
Show answer
Correct: B. zip pairs elements positionally from the iterables.
7. enumerate(['a', 'b']) yields:
- 'a', 'b'
- (0, 'a'), (1, 'b')
- (1, 'a'), (2, 'b')
- ['a', 'b']
Show answer
Correct: B. enumerate pairs each item with its index, starting at 0 by default.
8. In def f(*args, **kwargs), kwargs is:
- a list of positional args
- a dict of keyword args
- a single value
- the return value
Show answer
Correct: B. *args collects extra positional arguments as a tuple; **kwargs collects keyword arguments as a dict.
9. The slice a[1:4] returns the elements at indices:
- 1, 2, 3, 4
- 1, 2, 3
- 0, 1, 2, 3
- 2, 3, 4
Show answer
Correct: B. Slicing is start-inclusive and stop-exclusive: indices 1, 2, 3.
10. a[-1] refers to:
- an error
- the first element
- the last element
- index minus one, which is skipped
Show answer
Correct: C. Negative indices count from the end; -1 is the last element.
11. The main reason a NumPy array is faster than a Python list for numeric work:
- it uses more memory
- typed, contiguous storage with vectorized operations
- it is written in Java
- it caches results
Show answer
Correct: B. Homogeneous typed memory and vectorized C-level operations avoid slow per-element Python loops.
12. NumPy broadcasting aligns array shapes starting from:
- the leading (first) dimension
- the trailing (last) dimension
- the largest dimension
- alphabetical order
Show answer
Correct: B. Shapes are compared from the trailing axis; sizes must be equal or one of them 1.
13. A Python list is ____ and a tuple is ____:
- immutable; mutable
- mutable; immutable
- both mutable
- both immutable
Show answer
Correct: B. Lists can be modified in place; tuples cannot.
14. The expression {k: v for k, v in pairs} is a:
- set comprehension
- generator
- dict comprehension
- lambda
Show answer
Correct: C. The {key: value for ...} form builds a dictionary.
15. A lambda in Python is:
- a class
- a small anonymous function
- a loop
- a module
Show answer
Correct: B. lambda defines a short, unnamed function, e.g. lambda x: x + 1.