要素の積集合

s1 = {0, 1, 2}
s2 = {1, 2, 3}
s3 = {2, 3, 4}

s_intersection = s1 & s2
print(s_intersection)

s_intersection = s1.intersection(s2)
print(s_intersection)

[vagrant@localhost python]$ python app.py
{1, 2}
{1, 2}

あ、これは面白い

s1 = {0, 1, 2}
s2 = {1, 2, 3}
s3 = {2, 3, 4}

s_difference = s1 - s2
print(s_difference)

s_difference = s2.difference(s1)
print(s_difference)

[vagrant@localhost python]$ python app.py
{0}
{3}

どちらか一方だけに含まれる要素
symmetric_difference

s_symmetric_difference = s1.symmetric_difference(s3)
print(s_symmetric_difference)

[vagrant@localhost python]$ python app.py
{0, 1, 3, 4}
すご。