Wednesday, September 4, 2024

PYTHON - SETS

 Sets:


Sets are unordered collections of unique elements.
They are defined using curly braces {} or the set() constructor.
Sets are useful for removing duplicates and performing set operations (union, intersection, difference, etc.).

In Python, a set is an unordered collection of unique elements. Sets are defined using curly braces {} or the set() constructor. Sets do not allow duplicate values, and they are primarily used for performing mathematical set operations like union, intersection, difference, and more. Here, I'll explain in detail about sets and their operations in Python.

Example:

my_set = {1, 2, 3, 4, 4}

1. Creating Sets:
You can create a set by enclosing a sequence of elements within curly braces {} or by using the set() constructor. Sets can contain elements of any data type.

my_set1 = {1, 2, 3, 4, 5}
my_set2 = set([3, 4, 5, 6, 7])

2. Adding Elements:
You can add elements to a set using the add() method. Sets do not allow duplicate values, so adding a duplicate will have no effect.

my_set = {1, 2, 3}
my_set.add(4)  # Adds 4 to the set
my_set.add(2)  # Does not add 2 (already in the set)

3. Removing Elements:
You can remove elements from a set using the remove() or discard() method. The remove() method raises a KeyError if the element does not exist, while discard() does nothing in such cases.

my_set = {1, 2, 3, 4}
my_set.remove(3)   # Removes 3 from the set
my_set.discard(2)  # Removes 2 from the set

4. Set Operations:

Sets support various set operations like union, intersection, difference, and symmetric difference.

Union (| or union()): Combines two sets, keeping only unique elements.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2  # or set1.union(set2)
Intersection (& or intersection()): Returns elements that are common to both sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1 & set2  # or set1.intersection(set2)
Difference (- or difference()): Returns elements in the first set but not in the second.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1 - set2  # or set1.difference(set2)
Symmetric Difference (^ or symmetric_difference()): Returns elements that are in either of the sets but not in both.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_diff_set = set1 ^ set2  # or set1.symmetric_difference(set2)

5. Checking Membership:
You can check if an element is a member of a set using the in keyword.

my_set = {1, 2, 3}
is_present = 2 in my_set  # Returns True

6. Length of a Set:
You can find the number of elements in a set using the len() function.

my_set = {1, 2, 3, 4}
length = len(my_set)  # Returns 4

7. Iterating Through Sets:
You can use a for loop to iterate through the elements of a set.

my_set = {1, 2, 3}
for item in my_set:
    print(item)

Sets are commonly used for tasks that require unique values or for performing set-related operations like checking for common elements between collections, finding unique elements, or eliminating duplicates. Sets are an essential part of Python's standard library for data manipulation.

Common operations on sets inclu
de:

Adding elements: add()
Removing elements: remove(), discard()
Set operations: union(), intersection(), difference(), symmetric_difference()
Finding length: len()
Iterating: for item in my_set

 

No comments:

Post a Comment