1/14
Practice flashcards covering key concepts related to dictionaries and sets in Python.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a dictionary in Python?
A collection of key-value pairs with unique keys.
What happens if you try to access a non-existent key in a dictionary?
Raises a KeyError.
How do you add a new key-value pair to a dictionary?
Use dictionary[key] = value.
Which method safely retrieves a value from a dictionary without raising an error if the key doesn’t exist?
dictionary.get(key, default).
What does the popitem() method do?
Removes the last added key-value pair.
Which operator merges two dictionaries into a new one?
| (pipe operator).
What is the output of {1: 'a', 2: 'b'}.values()?
['a', 'b'].
What is a set in Python?
A collection of unique, unordered elements.
How do you add an element to a set?
Use set.add(element).
What is the difference between remove() and discard() for sets?
remove() raises an error if the element doesn’t exist.
Which operator finds the intersection of two sets?
& (ampersand).
What does setA <= setB check?
If setA is a subset of setB.
What is the output of {x**2 for x in {1, 2, 3}}?
{1, 4, 9}.
Which method returns the symmetric difference of two sets?
set1.symmetric_difference(set2).
What does set() with no arguments create?
An empty set.