🌍 Python Programming · flashcards

Python Programming Strings, Collections And Comprehensions Flashcards

50 question-and-answer cards covering Strings, Collections And Comprehensions 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
~134Chars per answer
FreePrice

24 sample cards from the Strings, Collections And Comprehensions deck

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

  1. What is the difference between list.remove(x) and list.pop(i)?

    remove(x) deletes the first element equal to x (raises ValueError if absent); pop(i) removes and returns the element at index i (default last element).

  2. What is the difference between list.sort() and the built-in sorted(list)?

    sort() sorts the list in place and returns None; sorted() returns a new sorted list and leaves the original unchanged.

  3. How does list slicing assignment, e.g., lst[1:3] = [9], behave?

    It replaces the targeted slice with the new elements, which can change the list's length; here two elements become one.

  4. What is the difference between a shallow copy of a list (e.g., lst[:] or lst.copy()) and the assignment new = lst?

    new = lst creates another reference to the same list (changes affect both); a shallow copy creates a new list object with the same element references.

  5. What is a tuple and how does it differ from a list?

    A tuple is an ordered, immutable collection written with parentheses, e.g., (1, 2). Unlike lists, tuples cannot be changed after creation.

  6. How do you create a single-element tuple, and why is the comma essential?

    Write (x,) with a trailing comma. Without the comma, (x) is just x in parentheses, not a tuple.

  7. What is tuple packing and unpacking? Give an example.

    Packing groups values into a tuple: t = 1, 2, 3. Unpacking distributes them into variables: a, b, c = t. They are inverse operations.

  8. What does extended unpacking with a starred target do, e.g., a, *b, c = [1,2,3,4]?

    The starred variable b collects the middle elements as a list, so a=1, b=[2,3], c=4.

  9. Why are tuples sometimes used as dictionary keys when lists cannot be?

    Dictionary keys must be hashable. Tuples (of hashable items) are immutable and hashable, while lists are mutable and unhashable.

  10. What is a Python dictionary and what is its core structure?

    A dictionary is a mutable, unordered (insertion-ordered since 3.7) collection of key-value pairs written with braces, e.g., {'a': 1}. Keys must be unique and hashable.

  11. How do you safely retrieve a value from a dict without raising KeyError if the key is missing?

    Use d.get(key, default) — it returns the value if present or the default (None if not specified) instead of raising KeyError.

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

    It returns the value for key if it exists; otherwise it inserts key with the default value and returns that default.

  13. What does the dict.update() method do?

    It merges another dict (or key-value pairs) into the dictionary, adding new keys and overwriting existing ones with new values.

  14. What are the three dictionary view methods and what does each return?

    keys() returns a view of the keys, values() returns a view of the values, and items() returns a view of (key, value) tuples. Views are dynamic and reflect changes to the dict.

  15. How do you iterate over both keys and values of a dictionary in one loop?

    for k, v in d.items(): — items() yields (key, value) tuples that unpack into k and v.

  16. What is a set in Python and what two properties define it?

    A set is an unordered, mutable collection of unique, hashable elements written with braces, e.g., {1, 2, 3}. It cannot contain duplicates.

  17. Why must you use set() rather than {} to create an empty set?

    {} creates an empty dictionary, not a set. set() is the only way to create an empty set.

  18. What set operators correspond to union, intersection, difference, and symmetric difference?

    Union is | , intersection is & , difference is - , and symmetric difference is ^ .

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

    A frozenset is an immutable version of a set. Because it is hashable, it can be used as a dictionary key or as an element of another set.

  20. What is the general syntax of a list comprehension, including an optional condition?

    [expression for item in iterable if condition] — it builds a list by evaluating expression for each item that satisfies the optional condition.

  21. Write the comprehension syntax for a dictionary comprehension and a set comprehension.

    Dict: {key_expr: value_expr for item in iterable}. Set: {expr for item in iterable}. Both use braces; the dict form includes a colon.

  22. What is the difference between an iterable and an iterator in Python?

    An iterable is any object you can loop over (has __iter__), while an iterator is an object that produces values one at a time via __next__() and raises StopIteration when exhausted.

  23. What is a generator, and how does a generator expression differ syntactically from a list comprehension?

    A generator lazily yields values one at a time (saving memory). A generator expression uses parentheses, e.g., (x*x for x in range(5)), instead of the square brackets of a list comprehension.

  24. What do the built-in functional helpers map(), filter(), and zip() each do?

    map(func, iterable) applies func to every element; filter(func, iterable) keeps elements where func is truthy; zip(a, b) pairs up elements from multiple iterables into tuples. All return lazy iterators.

What this deck covers

This deck covers the Strings, Collections And Comprehensions portion of the Python Programming syllabus in question-and-answer form. Browse the full Python Programming syllabus to see how it fits with the rest.

Answers are written to be recallable, not just readable — averaging about 134 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.

Strings, Collections And Comprehensions flashcards FAQ

How many Strings, Collections And Comprehensions 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 Strings, Collections And Comprehensions cards cover?

They follow the Strings, Collections And Comprehensions portion of the Python Programming syllabus, in question-and-answer form.

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.