0% found this document useful (0 votes)
17 views

Python Set Operations and Functions

The document outlines various Python set operations including union, intersection, difference, and symmetric difference, providing examples for each. It also describes built-in set functions such as add(), discard(), remove(), and clear(), with accompanying examples. These operations and functions are essential for manipulating sets in Python.

Uploaded by

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

Python Set Operations and Functions

The document outlines various Python set operations including union, intersection, difference, and symmetric difference, providing examples for each. It also describes built-in set functions such as add(), discard(), remove(), and clear(), with accompanying examples. These operations and functions are essential for manipulating sets in Python.

Uploaded by

gp249780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Set Operations and Built-in Functions

1. Set Union: The union of two sets is the set of all the elements of both the sets without duplicates.

Example:

>>> first_set = {1, 2, 3}

>>> second_set = {3, 4, 5}

>>> first_set.union(second_set)

{1, 2, 3, 4, 5}

2. Set Intersection: The intersection of two sets is the set of all the common elements of both the

sets.

Example:

>>> first_set = {1, 2, 3, 4, 5, 6}

>>> second_set = {4, 5, 6, 7, 8, 9}

>>> first_set.intersection(second_set)

{4, 5, 6}

3. Set Difference: The difference between two sets is the set of all the elements in first set that are

not present in the second set.

Example:

>>> first_set = {1, 2, 3, 4, 5, 6}

>>> second_set = {4, 5, 6, 7, 8, 9}

>>> first_set.difference(second_set)

{1, 2, 3}

>>> second_set - first_set

{8, 9, 7}
4. Set Symmetric Difference: The symmetric difference between two sets is the set of all the

elements that are either in the first set or the second set but not in both.

Example:

>>> first_set = {1, 2, 3, 4, 5, 6}

>>> second_set = {4, 5, 6, 7, 8, 9}

>>> first_set.symmetric_difference(second_set)

{1, 2, 3, 7, 8, 9}

1. add(): Adds an element to the set. If the element already exists, does nothing.

Example:

>>> s = {'g', 'e', 'k', 's'}

>>> s.add('f')

>>> print(s)

{'g', 'e', 'k', 's', 'f'}

2. discard(): Removes an element from the set. If it does not exist, does nothing.

Example:

>>> s = {'g', 'e', 'k', 's'}

>>> s.discard('g')

>>> print(s)

{'e', 'k', 's'}

3. remove(): Removes a specific element from the set. Raises an error if the element is not found.

Example:

>>> s = {'g', 'e', 'k', 's'}

>>> s.remove('e')

>>> print(s)
{'g', 'k', 's'}

4. clear(): Removes all elements from the set.

Example:

>>> s = {'g', 'e', 'k', 's'}

>>> s.clear()

>>> print(s)

set()

You might also like