Python Set Operations With Examples

Set Operations

Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

Union

Union of two sets is a set containing all elements of both sets.

set_a | set_b

or

set_a.union(sequence)

union() converts sequence to a set, and performs the union.

Example 1

set_a = {4, 2, 8}
set_b = {1, 2}
union = set_a | set_b
print(union)

Output

{1, 2, 4, 8}

Example 2

set_a = {4, 2, 8}
list_a = [1, 2]
union = set_a.union(list_a)
print(union)

Output

{1, 2, 4, 8}

Intersection

Intersection of two sets is a set containing common elements of both sets.

set_a & set_b

or

set_a.intersection(sequence)

intersection() converts sequence to a set, and perform the intersection.

Example 1

set_a = {4, 2, 8}
set_b = {1, 2}
intersection = set_a & set_b
print(intersection)

Output

{2}

Example 2

set_a = {4, 2, 8}
list_a = [1, 2]
intersection = set_a.intersection(list_a)
print(intersection)

Output

{2}

Difference

Difference of two sets is a set containing all the elements in the first set but not second.

set_a – set_b

or

set_a.difference(sequence)

difference() converts sequence to a set.

Example 1

set_a = {4, 2, 8}
set_b = {1, 2}
diff = set_a - set_b
print(diff)

Output

{8, 4}

Example 2

set_a = {4, 2, 8}
tuple_a = (1, 2)
diff = set_a.difference(tuple_a)
print(diff)

Output

{8, 4}

Symmetric Difference

Symmetric difference of two sets is a set containing all elements which are not common to both sets.

set_a ^ set_b

or

set_a.symmetric_difference(sequence)

symmetric_difference() converts sequence to a set.

Example 1

set_a = {4, 2, 8}
set_b = {1, 2}
symmetric_diff = set_a ^ set_b
print(symmetric_diff)

Output

{8, 1, 4}

Example 2

set_a = {4, 2, 8}
set_b = {1, 2}
diff = set_a.symmetric_difference(set_b)
print(diff)

Output

{8, 1, 4}

Related Posts

Python Program to Convert Integer into a Single Numeral

Single Digit Number Ram is given a positive integer N .He wishes to convert this integer into a single numeral. He does so by repeatedly adding the…

Python Program to Print Armstrong numbers between two intervals

Write a program to print all the Armstrong numbers in the given range A to B (including A and B). An N – digit number is an…

Python Program to Check if Given Input is a Letter, Digit or Special Character

Letter, Digit or Special Character You are given a character as input. Check if the given input is a Lowercase Letter or Uppercase Letter or Digit or…

Interleave Strings | Python Program to merge two strings by adding characters in alternating order

Interleave Strings Given two strings, write a program to merge the given two strings by adding characters in alternating order, starting with the first string. If a…

Python Program to Check if given colors are present in the color palate

Shekhar is an artist. He likes to draw and paint. He is now drawing a portrait of a city road which is in the black color and…

Python Program to Check the eligibility criteria for donor  

Python Comparison Operators An XYZ hospital has organised a blood donation camp. The eligibility criteria for donor was as follows  1) Age>=18  2) Weight >=50 kg  3)…

Leave a Reply

Your email address will not be published.