- AFalse False
- BTrue True
The difference()
method returns a set that contains the difference between two sets.
Here set3 = set2.difference(set1)
so the returned set contains items that exist only in the first set, and not in both sets.
The remove()
and discard()
method removes the specified item from a set.
Python set
doesn’t allow duplicate items.
If the item to remove does not exist in the set, the discard()
method will NOT raise an error. If we use remove()
method to perform the same operation, we will receive a keyError
.
We can update multiple items using the update()
method.
We cannot access items in a set by referring to an index because the ‘set’ object does support indexing (the set is unordered). if you try to access items using the index, you will get TypeError: ‘set’ object does not support indexing.
Use a for loop to access set items.
sampleSet = {"Yellow", "Orange", "Black"} for item in sampleSet: print(item)
When you set set2= set11
, you are making them refer to the same dict object, so when you modify one of them, all references associated with that object reflect the current state of the object. So don’t use the assignment operator to copy the set instead use the copy()
method or set()
constructor.