union may receive any iterable as its argument (this is true for subsequent methods in this section as well).{1, 3, 5} | {2, 3, 4}
{1, 3, 5}.union([20, 20, 3, 40, 40])
The intersection of two sets is a set consisting of all the unique elements that the two sets have in common.
{1, 3, 5} & {2, 3, 4}
{1, 3, 5}.intersection([1, 2, 2, 3, 3, 4, 4])
The difference between two sets is a set consisting of the elements in the left operand that are not in the right operand.
{1, 3, 5} - {2, 3, 4}
{1, 3, 5, 7}.difference([2, 2, 3, 3, 4, 4])
The symmetric difference between two sets is a set consisting of the elements of both sets that are not in common with one another.
{1, 3, 5} ^ {2, 3, 4}
{1, 3, 5, 7}.symmetric_difference([2, 2, 3, 3, 4, 4])
Two sets are disjoint if they do not have any common elements.
{1, 3, 5}.isdisjoint({2, 4, 6})
{1, 3, 5}.isdisjoint({4, 6, 1})
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 6 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.