The .update() method adds every item in the set being passed as an argument (friend_salad0 to the set that .update() is being called on (fruit_salad). The built in method len() in this case returns the number of elements in the set, also known as the cardinality.
If your salad recipe has three fruits and your friend's has four, why does the combined recipe have six fruits and not seven?
Note that the same result could have been accomplished with
However, in practice, .discard is rarely used because the suppression of the error message increases the risk of an uncaught bug in the set itself being propagated through the code.
You can make your code more robust by checking that the element is already inside before discarding.
if 'raspberry' in fruit_salad:fruit_salad.remove('raspberry')
Your brother doesn't like kiwi. He is familiar with most list methods, including .pop(). He infers that if he calls .pop() on your salad, the kiwi will disappear since it's the "last" element in the set. Before trying this out in your console, do you agree or disagree with your brother?
Well, as it turns out, he was wrong. The .pop() method removes an element from a set and returns it. However, your brother forgot that sets, unlike lists, are unordered, so there's no way to know the order in which the items are being stored; instead, Python will remove an item at random.
And now, after the fruit salad has been finished, don't forget to clear the dishes:
>>> fruit_salad.clear()>>> fruit_saladset()
The .clear() method removes all the elements from a set, but keeps it in memory. It is not to be confused with
which removes fruit_salad from the variable space entirely.