Introduction to Deep Learning · HIT

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.

Core Python

Idiomatic and advanced features

Useful dunder (magic) methods

NumPy versus Python lists

What recurs in PyTorch and DL code

Readiness check

Self-check questions

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:

  1. [0, 1, 2, 3]
  2. [1, 4, 9, 16]
  3. [0, 1, 4, 9]
  4. [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:

  1. return
  2. yield
  3. lambda
  4. 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:

  1. faster indexing
  2. lazy, memory-efficient iteration
  3. automatic sorting
  4. 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]?

  1. __init__ and __str__
  2. __len__ and __getitem__
  3. __call__ and __repr__
  4. __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:

  1. printing the object
  2. calling the instance like a function: obj(x)
  3. adding two objects
  4. 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. (1, 2, 3, 'a', 'b', 'c')
  2. (1, 'a'), (2, 'b'), (3, 'c')
  3. a dictionary
  4. [1, 'a', 2, 'b']
Show answer
Correct: B. zip pairs elements positionally from the iterables.

7. enumerate(['a', 'b']) yields:

  1. 'a', 'b'
  2. (0, 'a'), (1, 'b')
  3. (1, 'a'), (2, 'b')
  4. ['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:

  1. a list of positional args
  2. a dict of keyword args
  3. a single value
  4. 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. 1, 2, 3, 4
  2. 1, 2, 3
  3. 0, 1, 2, 3
  4. 2, 3, 4
Show answer
Correct: B. Slicing is start-inclusive and stop-exclusive: indices 1, 2, 3.

10. a[-1] refers to:

  1. an error
  2. the first element
  3. the last element
  4. 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:

  1. it uses more memory
  2. typed, contiguous storage with vectorized operations
  3. it is written in Java
  4. 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:

  1. the leading (first) dimension
  2. the trailing (last) dimension
  3. the largest dimension
  4. 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 ____:

  1. immutable; mutable
  2. mutable; immutable
  3. both mutable
  4. 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:

  1. set comprehension
  2. generator
  3. dict comprehension
  4. lambda
Show answer
Correct: C. The {key: value for ...} form builds a dictionary.

15. A lambda in Python is:

  1. a class
  2. a small anonymous function
  3. a loop
  4. a module
Show answer
Correct: B. lambda defines a short, unnamed function, e.g. lambda x: x + 1.

📚Refresher resources

Refresh
Refresh
Refresh

← All prerequisites Course home

PreviousMathematicsNextBasic Machine Learning Concepts