1/19
Looks like no tags are added yet.
What is a set in python?
An unordered collection of unique elements. Duplicates are removed.
How do you create an empty set?
set()- {} creates an empty dictionary
Can you access a set using an index?
No. Sets are unordered and don’t support numerical indexing
What does A\|B do
Union: all unique elements in either set
What does A & B do
Intersection: elements found in both sets
What does A - B do?
Difference: elements in A but not B
What does A ^ B do?
Symmetric difference: elements in either set, but not in both
What happens when you convert a list into a set?
Duplicate elements are removed and order is not guaranteed
What is dictionary?
A collection of key value pairs. Each key must be unique
How do you access a dictionary value?
d[key]. For example: d[“name”]
What happens if you access a nonexistent key with d[key]?
Python raises a KeyError
What does d.get(key, 0) do?
Returns the value if they key exists; otherwise returns 0
How do you add or update a dictionary entry?
d[key] = value - adds a new key or updates an existing one
What does len(d) return?
The number of key-value pairs in the dictionary
What did key in d check?
Whether the key exists in the dictionary, not whether it’s a value
What does d.keys() return?
A view of the dictionary’s keys
What does d.values() return?
A view of the dictionary’s values
What does for key in d: iterate over?
The dictionary’s keys
What does d[key] = d.get(key, 0) + 1 do?
Increments the count for a key, starting at 0 if it’s missing
How do you loop through a dictionary’s keys and values?
for key, value in d.items():