🌍 Python Programming · flashcards

Python Programming Data Structures and Collections Flashcards

50 question-and-answer cards covering Data Structures and Collections as it is examined in Python Programming. 24 of them are printed below, taken from across the deck — no signup, no paywall on the preview.

50Cards in deck
24Free preview
21Syllabus topics
~150Chars per answer
FreePrice

24 sample cards from the Data Structures and Collections deck

Sampled from the end of the deck, so these are different cards from the ones shown on the syllabus page.

  1. How do you create a dictionary, and what constitutes each entry?

    Use braces with key-value pairs: `d = {'a': 1, 'b': 2}` (or `dict(a=1, b=2)`). Each entry maps a unique, hashable key to a value; keys cannot repeat.

  2. What requirement must dictionary keys satisfy?

    Keys must be hashable (immutable types like strings, numbers, or tuples of immutables). Lists and other dicts cannot be keys because they are unhashable/mutable.

  3. How do you access a value by key, and what is the difference between `d['x']` and `d.get('x')`?

    `d['x']` returns the value but raises `KeyError` if the key is missing. `d.get('x')` returns `None` (or a supplied default `d.get('x', default)`) instead of raising.

  4. How do you add or update a key-value pair in a dictionary?

    Assign to the key: `d['c'] = 3`. If the key exists it updates the value; if not, it inserts a new entry.

  5. What does `dict.setdefault(key, default)` do?

    It returns the value for `key` if present; otherwise it inserts `key` with `default` and returns `default`. It is handy for initializing entries (e.g. building lists per key).

  6. What do `dict.keys()`, `dict.values()`, and `dict.items()` return?

    View objects: `keys()` gives the keys, `values()` gives the values, and `items()` gives `(key, value)` tuples. Views are dynamic and reflect later changes to the dictionary.

  7. What is the difference between `dict.pop(key)` and `dict.popitem()`?

    `pop(key)` removes and returns the value for a given key (raises `KeyError` if absent, unless a default is given). `popitem()` removes and returns the last inserted `(key, value)` pair.

  8. How do you merge two dictionaries `a` and `b` in Python 3.9+?

    Use the merge operator: `a | b` creates a new merged dict, or `a |= b` updates `a` in place. Alternatively `a.update(b)` mutates `a`. On key conflicts, `b`'s values win.

  9. Write a dictionary comprehension that maps numbers 0–3 to their squares.

    `{x: x**2 for x in range(4)}` produces `{0:0, 1:1, 2:4, 3:9}`. General form: `{key_expr: value_expr for item in iterable}`.

  10. How do you invert a dictionary `d` (swap keys and values) with a comprehension?

    `{v: k for k, v in d.items()}`. Note that values must be hashable and unique, or entries will collide and be overwritten.

  11. When you iterate directly over a dictionary with `for x in d:`, what does `x` bind to?

    `x` binds to each key. To iterate over values use `for v in d.values()`, and for both use `for k, v in d.items()`.

  12. In modern Python (3.7+), what ordering guarantee do dictionaries provide during iteration?

    Dictionaries preserve insertion order, so iteration yields keys in the order they were first inserted. This is a language guarantee as of Python 3.7.

  13. How do you create a set, and what happens to duplicate values?

    Use braces of values: `s = {1, 2, 3}` (or `set(iterable)`). Duplicates are automatically discarded, so `{1, 2, 2, 3}` becomes `{1, 2, 3}`. Sets are unordered and mutable.

  14. How do you create an empty set, and why can't you use `{}`?

    Use `set()`. `{}` creates an empty dictionary, not a set, because dict syntax claimed the empty-braces notation.

  15. What are the operators for union, intersection, difference, and symmetric difference of sets?

    Union: `A | B`; intersection: `A & B`; difference: `A - B`; symmetric difference: `A ^ B` (elements in exactly one set). Each also has a method form (`union`, `intersection`, etc.).

  16. For `A = {1,2,3}` and `B = {2,3,4}`, evaluate `A & B`, `A | B`, `A - B`, and `A ^ B`.

    `A & B` = {2,3}; `A | B` = {1,2,3,4}; `A - B` = {1}; `A ^ B` = {1,4}.

  17. How do you test whether set `A` is a subset of set `B`?

    Use `A <= B` or `A.issubset(B)`, which is True if every element of `A` is in `B`. Use `A < B` for a proper (strict) subset.

  18. What is a frozenset and how does it differ from a set?

    A `frozenset` is an immutable version of a set, created with `frozenset(iterable)`. It supports set operations but not mutation methods (no `add`/`remove`), and being hashable it can be a dict key or an element of another set.

  19. Why can a frozenset be an element of a regular set but a regular set cannot?

    Set elements must be hashable. A regular `set` is mutable and therefore unhashable, while a `frozenset` is immutable and hashable, so only the frozenset can be nested.

  20. How do you add and remove elements from a mutable set?

    Add with `s.add(x)`. Remove with `s.remove(x)` (raises `KeyError` if absent) or `s.discard(x)` (no error if absent); `s.pop()` removes and returns an arbitrary element.

  21. How does string indexing work, and what does `'Python'[0]` return?

    Strings are indexed like sequences: `'Python'[0]` = 'P'. Indexing returns a one-character string; negative indices count from the end, e.g. `'Python'[-1]` = 'n'.

  22. What does `'Python'[1:4]` return, and can you modify a character via `s[0] = 'X'`?

    `'Python'[1:4]` = 'yth' (indices 1,2,3). You cannot do `s[0] = 'X'` — strings are immutable, so item assignment raises `TypeError`.

  23. How do you reverse a string and how do you take every second character using slicing?

    Reverse: `s[::-1]`. Every second character: `s[::2]`. Slicing with a step controls direction and stride.

  24. Compare lists, tuples, sets, and dictionaries by ordering, mutability, and duplicates.

    Lists: ordered, mutable, allow duplicates. Tuples: ordered, immutable, allow duplicates. Sets: unordered, mutable, no duplicates. Dictionaries: ordered (3.7+), mutable, unique keys mapping to values.

What this deck covers

The Data Structures and Collections deck follows the Python Programming Data Structures and Collections syllabus — 5 chapters and 21 topics — so questions land on material that is genuinely examinable rather than trivia around it. That works out to roughly 10.0 cards per chapter.

Answers are written to be recallable, not just readable — averaging about 150 characters, which is long enough to carry the reasoning and short enough to say out loud.

A deck like this earns its keep on the second and third pass. Read the syllabus first so you know the shape of the subject, then use the cards to find the specific facts that have not stuck.

Data Structures and Collections flashcards FAQ

How many Data Structures and Collections flashcards are in this Python Programming deck?

50 cards. This page previews 24 of them, sampled evenly across the deck so you can judge the difficulty before installing anything.

Are these Python Programming flashcards free?

Yes. The preview here is free to read with no signup, and the full 50-card deck is free inside the Examius app.

What do the Data Structures and Collections cards cover?

They follow the Python Programming Data Structures and Collections syllabus — 5 chapters and 21 topics — so the questions track what is actually examinable.

How should I use these flashcards?

Read the syllabus first so you know the shape of the subject, then drill the deck. Examius schedules each card with spaced repetition, so cards you keep missing come back sooner and ones you know drift further apart.