Sets in python: 25 flashcards
All 25 flashcards from this kit, free and with no sign-up.
1. What is a set in Python?
Show the answer
An unordered collection of unique, immutable elements.
2. How do you create an empty set?
Show the answer
Use `set()`. Using `{}` creates an empty dictionary.
3. What is the output of `{1, 2, 3} - {2, 3}`?
Show the answer
`{1}` (difference: elements in first but not second).
4. What does `{1, 2} | {2, 3}` return?
Show the answer
`{1, 2, 3}` (union: all unique elements).
5. What does `{1, 2} & {2, 3}` return?
Show the answer
`{2}` (intersection: common elements).
6. What does `{1, 2} ^ {2, 3}` return?
Show the answer
`{1, 3}` (symmetric difference: elements in either but not both).
7. How do you add a single element to a set?
Show the answer
Use `.add(element)`.
8. How do you remove an element from a set, raising an error if missing?
Show the answer
Use `.remove(element)`.
9. How do you remove an element from a set without error if missing?
Show the answer
Use `.discard(element)`.
10. What does `{1, 2}.issubset({1, 2, 3})` return?
Show the answer
`True` (all elements of first are in second).
11. What does `{1, 2, 3}.issuperset({1, 2})` return?
Show the answer
`True` (first contains all elements of second).
12. How to check if two sets have no common elements?
Show the answer
Use `.isdisjoint(other_set)`. Returns `True` if intersection is empty.
13. Can a list be an element of a set? Why?
Show the answer
No, because lists are mutable (unhashable). Sets require immutable elements.
14. What is a frozenset?
Show the answer
An immutable version of a set, created with `frozenset()`. It is hashable and can be used as a dictionary key or set element.
15. How to get the number of unique elements in a list using a set?
Show the answer
`len(set(my_list))`
16. What is the time complexity of membership testing (`in`) for a set?
Show the answer
Average O(1) due to hash table implementation.
17. What is the output of `{x**2 for x in range(3)}`?
Show the answer
`{0, 1, 4}` (set comprehension).
18. What happens if you try to add a mutable element like a list to a set?
Show the answer
Python raises a TypeError because sets require hashable (immutable) elements.
19. How do you perform a union of two sets using a method?
Show the answer
Use `set1.union(set2)` which returns a new set with elements from both sets.
20. What is the difference between `set.discard()` and `set.remove()`?
Show the answer
Both remove an element, but `remove()` raises a KeyError if the element is missing, while `discard()` does nothing.
21. How can you update a set with elements from another set without creating a new set?
Show the answer
Use `set1.update(set2)` which adds all elements from set2 to set1 in place.
22. What does `{1, 2}.isdisjoint({3, 4})` return?
Show the answer
True, because the two sets have no elements in common.
23. Can a frozenset be an element of a set?
Show the answer
Yes, because frozensets are immutable and hashable.
24. How do you create a set from a string, and what does it contain?
Show the answer
Use `set('hello')` which returns {'h', 'e', 'l', 'o'} (unordered, unique characters).
25. What is the output of `{1, 2, 3}.symmetric_difference({2, 3, 4})`?
Show the answer
`{1, 4}` (elements in exactly one of the sets).
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages