Back to questions
JuniorTheoryCommon
What does the program print?
a = {1, 2, 3}
b = {2, 3, 4}
print(len(a | b), len(a & b), sorted(a - b))4 2 [1]
- Expecting
6, as if|joined the sets end to end: a set holds each value once, soa | bis{1, 2, 3, 4}with4elements. - Expecting
[1, 4]fora - b: the difference keeps what is inaand not inb; elements found only inbare dropped.
- Which operator gives the elements that are in exactly one of the two sets?
- Why does the program wrap
a - binsortedbefore printing?