Python Program to Perform Union of Two Sets

Introduction

Sets in Python are collections of unique elements, and they support various mathematical operations such as union, intersection, difference, and symmetric difference. The union of two sets is a set that contains all the elements from both sets, with duplicates removed. This tutorial will guide you through creating a Python program that demonstrates how to perform the union of two sets.

Example:

  • Set 1: {'apple', 'banana', 'cherry'}
  • Set 2: {'cherry', 'date', 'elderberry'}
  • Union: {'apple', 'banana', 'cherry', 'date', 'elderberry'}

Problem Statement

Create a Python program that:

  • Takes two sets as input.
  • Performs the union of the two sets.
  • Displays the resulting set after the union operation.

Solution Steps

  1. Create Two Sets: Initialize two sets with some elements.
  2. Perform the Union of the Two Sets: Use the union() method or the | operator to combine the two sets.
  3. Display the Union Set: Use the print() function to display the resulting set after the union operation.

Python Program

# Python Program to Perform Union of Two Sets
# Author: https://www.rameshfadatare.com/

# Step 1: Create two sets with elements
set1 = {'apple', 'banana', 'cherry'}
set2 = {'cherry', 'date', 'elderberry'}

# Step 2: Perform the union of the two sets using the union() method
union_set = set1.union(set2)

# Step 3: Display the resulting union set
print("The union of set1 and set2 is:", union_set)

# Alternatively, you can use the | operator to perform the union
union_set_alternative = set1 | set2
print("The union of set1 and set2 using | operator is:", union_set_alternative)

Explanation

Step 1: Create Two Sets with Elements

  • Two sets, set1 and set2, are created with elements: {'apple', 'banana', 'cherry'} and {'cherry', 'date', 'elderberry'}, respectively. Sets are defined using curly braces {}.

Step 2: Perform the Union of the Two Sets Using the union() Method

  • The union() method is used to combine the elements of set1 and set2 into a new set union_set. The union operation ensures that all elements are unique, with duplicates removed.

Step 3: Display the Resulting Union Set

  • The print() function is used to display the resulting set after the union operation.

Alternative: Use the | Operator

  • The | operator is an alternative way to perform the union of two sets. The result will be the same as using the union() method.

Output Example

Example Output:

The union of set1 and set2 is: {'apple', 'cherry', 'elderberry', 'date', 'banana'}
The union of set1 and set2 using | operator is: {'apple', 'cherry', 'elderberry', 'date', 'banana'}

(Note: The order of elements in the output may vary because sets are unordered collections.)

Additional Examples

Example 1: Union of Two Sets with Common and Unique Elements

# Sets with common and unique elements
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

# Perform union
union_set = set1.union(set2)
print("Union of set1 and set2:", union_set)

Output:

Union of set1 and set2: {1, 2, 3, 4, 5, 6}

Example 2: Union of Disjoint Sets

# Disjoint sets (no common elements)
set1 = {'red', 'blue'}
set2 = {'green', 'yellow'}

# Perform union
union_set = set1.union(set2)
print("Union of disjoint sets:", union_set)

Output:

Union of disjoint sets: {'red', 'green', 'blue', 'yellow'}

Example 3: Union of an Empty Set with a Non-Empty Set

# One empty set and one non-empty set
set1 = set()
set2 = {'alpha', 'beta', 'gamma'}

# Perform union
union_set = set1.union(set2)
print("Union of an empty set with a non-empty set:", union_set)

Output:

Union of an empty set with a non-empty set: {'alpha', 'beta', 'gamma'}

Conclusion

This Python program demonstrates how to perform the union of two sets using both the union() method and the | operator. The union operation is a fundamental set operation that combines the elements of two sets while removing duplicates. Understanding how to perform this operation is essential for tasks involving set theory and data management in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top