Sets in Python

 Sets in Python

Sets in Python are unordered collections of unique elements in Python. They are similar to lists but do not allow duplicate elements. Sets are defined using curly braces {} or the set() constructor.

Sets in Python

Key Characteristics:

  • Unordered: Elements in a set do not have a specific order.
  • Unique Elements: Sets cannot contain duplicate elements.
  • Mutable: Sets can be modified by adding or removing elements.
  • Iterable: You can iterate over the elements of a set using a loop.
Creating Sets:
# Creating a set using curly braces
my_set = {1, 2, 3, 4, 5}

# Creating a set using the set() constructor
my_set = set([1, 2, 3, 4, 5])

Creating a set

The set can be created by enclosing the comma-separated immutable items with the curly braces {}. Python also provides the set() method, which can be used to create the set by the passed sequence.

Example 1: Using curly braces

  1. Days = {"Monday""Tuesday""Wednesday""Thursday""Friday""Saturday""Sunday"}    
  2. print(Days)    
  3. print(type(Days))    
  4. print("looping through the set elements ... ")    
  5. for i in Days:    
  6.     print(i)    

Output:

{'Friday', 'Tuesday', 'Monday', 'Saturday', 'Thursday', 'Sunday', 'Wednesday'}
<class 'set'>
looping through the set elements ...
Friday
Tuesday
Monday
Saturday
Thursday
Sunday
Wednesday

Accessing Elements:

You cannot access elements in a set by index, as they are unordered. However, you can check if an element exists in a set using the in operator:

if 3 in my_set:

    print("3 is in the set")


Adding and Removing Elements:

You can add elements to a set using the add() method and remove elements using the remove() method. If you try to add an element that already exists in the set, it will have no effect.

my_set.add(6)

my_set.remove(2)

Set Operations:

Sets support various operations, including:

  • Union: | operator or union() method: Creates a new set containing all elements from both sets.
  • Intersection: & operator or intersection() method: Creates a new set containing elements common to both sets.
  • Difference: - operator or difference() method: Creates a new set containing elements present in the first set but not in the second.
  • Symmetric Difference: ^ operator or symmetric_difference() method: Creates a new set containing elements that are in either set, but not both.
set1 = {1, 2, 3}
set2 = {2, 3, 4}

# Union: {1, 2, 3, 4}
union_set = set1 | set2

# Intersection: {2, 3}
intersection_set = set1 & set2

# Difference: {1}
difference_set = set1 - set2

# Symmetric Difference: {1, 4}
symmetric_difference_set = set1 ^ set2

Conclusion:

A Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation.

Unlike other collections in Python, there is no index attached to the elements of the set, i.e., we cannot directly access any element of the set by the index. However, we can print them all together, or we can get the list of elements by looping through the set.

Comments

Popular posts from this blog

Python to automate What's App messages

Install WAMP server to run python

Building RESTful APIs with Flask in PyCharm