0% found this document useful (0 votes)
7 views2 pages

Python Set Operations Explained

The document outlines a Python program that demonstrates various set operations including union, intersection, difference, and symmetric difference using two sets, A and B. It provides the source code along with the expected output for each operation. The program also checks if one set is a subset or superset of the other.

Uploaded by

adithapa129
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Python Set Operations Explained

The document outlines a Python program that demonstrates various set operations including union, intersection, difference, and symmetric difference using two sets, A and B. It provides the source code along with the expected output for each operation. The program also checks if one set is a subset or superset of the other.

Uploaded by

adithapa129
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Student name- Sachin Page no-3

Roll no- 48 D.O.P-26/08/2025


Course- BCA AI&DS

PROBLEM STATEMENT 3- Write a python program to implement set operation.

OBJECTIVE- To understand the implementation of various set operation.

EXPLANATION- A set operation is a mathematical action on two or more sets that

Produces a new set by combining, comparing, or modifying the elements of the

original sets.

SOURCE CODE-

# Creating two sets

A = {1, 2, 3, 4, 5}

B = {4, 5, 6, 7, 8}

print("Set A:", A)

print("Set B:", B)

# Union

print("\nUnion of A and B:", A | B) # or [Link](B)

# Intersection

print("Intersection of A and B:", A & B) # or [Link](B)

# Difference

print("Difference of A and B (A - B):", A - B) # or [Link](B)

print("Difference of B and A (B - A):", B - A)

# Symmetric Difference

print("Symmetric Difference of A and B:", A ^ B) # or A.symmetric_difference(B))

# Subset and Superset

print("\nIs A subset of B?", [Link](B))

print("Is A superset of B?", [Link](B))


Student name- Sachin Page no-3
Roll no- 48 D.O.P-26/08/2025
Course- BCA AI&DS

OUTPUT-

Set A: {1, 2, 3, 4, 5}

Set B: {4, 5, 6, 7, 8}

Union of A and B: {1, 2, 3, 4, 5, 6, 7, 8}

Intersection of A and B: {4, 5}

Difference of A and B (A - B): {1, 2, 3}

Difference of B and A (B - A): {8, 6, 7}

Symmetric Difference of A and B: {1, 2, 3, 6, 7, 8}

Is A subset of B? False

Is A superset of B? False

Common questions

Powered by AI

In set operations, the standard difference (A - B) produces a set of elements that are in A but not in B, resulting in {1, 2, 3} for sets A and B. Conversely, the symmetric difference operation results in a set containing elements present in either A or B, but not in both. For the given example, the symmetric difference of A and B is {1, 2, 3, 6, 7, 8}, illustrating elements that are not shared between the two sets .

A set operation in mathematics is an action performed on two or more sets that produces a new set by combining, comparing, or modifying the elements of the original sets. In programming, set operations are used to manage and manipulate collections of items, allowing for operations such as union, intersection, difference, and symmetric difference to efficiently handle data .

A Python function to calculate the symmetric difference could be: def symmetric_difference(A, B): return A ^ B. This operation is practically useful in scenarios where there is a need to find distinct elements between two datasets, such as identifying items that are unique to either dataset, which is common in data analytics and reconciliation processes .

The union operation can be implemented in Python using the union() method or the '|' operator. For sets A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7, 8}, the union operation yields a new set {1, 2, 3, 4, 5, 6, 7, 8} by combining all unique elements from both sets .

To filter out common elements between two lists using set difference, convert both lists to sets and then perform the difference operation. A sample code is: def filter_common(list1, list2): set1 = set(list1); set2 = set(list2); return list(set1 - set2). For lists [1, 2, 3, 4, 5] and [4, 5, 6], the resulting list would be [1, 2, 3], as these are the elements in list1 not present in list2 .

A set is a subset of another set if all elements of the first set are contained within the second set. It is a superset if it contains all elements of another set. In the given problem, neither set A nor set B is a subset or a superset of each other, as there are elements in both sets that are not contained in the other .

Union and difference operations serve distinct roles in data preprocessing. Union combines datasets, ensuring no duplicate entries, and is used when merging records from different sources. On the contrary, difference eliminates elements of one dataset from another, useful for deduplicating or excluding certain entries. Choosing between them depends on task requirements such as consolidation versus exclusion, making them integral components of data curation and analysis .

The intersection operation in Python can be performed using the intersection() method or the '&' operator. It identifies common elements between two sets. In the given example with sets A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7, 8}, the intersection operation results in the set {4, 5}, which are the elements common to both sets .

Implementing set operations in Python enhances computational efficiency by leveraging the inherent properties of sets, which automatically remove duplicates and support faster operations like union, intersection, and difference via optimized hashing mechanisms. These operations are particularly useful when dealing with large datasets to quickly filter or combine data without manually iterating over elements .

The performance of set operations in Python is greatly impacted by the choice of data structure. Python sets are implemented as hash tables, allowing for average O(1) complexity on membership tests and basic operations such as union and intersection. This makes them ideal for tasks involving unpacking large data into unique elements. The hash-based structure ensures efficient data retrieval and manipulation, crucial for performance-heavy applications where speed and memory efficiency are paramount .

You might also like