Python  

Python - Data Types - Set Operations

In the last session, we discussed the Tuples. Now, we will look at Python's Set Operations. The set's are the most important topic in Python.

Let’s start,

Definition of Set in Python

  • A set is an Immutable (unchangeable) data structure.

  • It is represented using Curly braces{ }.

  • Set are used to store multiple values in a single variable.

  • They can hold different data types such as integers, floats, strings, and Boolean values.

  • Set support for adding and deleting elements.

  • Set can't allow duplicate values.

  • Commas separate multiple values in a list ( , ) .

  • Set is an unordered collection

  • Set can perform operations like unions, intersections, and differences.

  1. Introduction to set

#input
my_set={1,2,3,4,5}
print(my_set)

#output
{1,2,3,4,5}
  1. Check the Type of set

#input
my_set={1,2,3,4,5}
print(type(my_set))

#output
<class 'set'>
  1. List converted into set

#input
my_set=set([1,2,3,4,5,6])
print(my_set)

#output
{1, 2, 3, 4, 5, 6}
  1. List converted to set and unique values

#input
my_set=set([1,2,3,6,5,4,5,6])
print(my_set)

#output
{1, 2, 3, 4, 5, 6}
  1. Set adding Operations

#input
my_set={1,2,3,4,5,6}
my_set.add(7)
print(my_set)
my_set.add(7) #2nd time adding
print(my_set)

#output
{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5, 6, 7} # same answer because set allowed only unique values
  1. Remove the elements from a set

#input
my_set={1,2,3,4,5,6,7}
my_set.remove(3)
print(my_set)

#output
{1, 2, 4, 5, 6, 7}
  1. set using "pop" method

#input
my_set={1,2,3,4,5,6,7}
my_set_pop =my_set.pop()
print(my_set_pop)

#output
{2, 3, 4, 5, 6, 7}
  1. Clear the set

#input
my_set.clear()
print(my_set)

#output
set()
  1. Set Membership Operations

#input
my_set={1,2,3,4,5}
print(3 in my_set)
print(10 in my_set)

#output
True
False
  1. Set Mathematical Union Operations

#input
set1={1,2,3,4,5,6}
set2={4,5,6,7,8,9}

## Intersection
intersection_set=set1.intersection(set2)
print(intersection_set)

#output
{1, 2, 3, 4, 5, 6, 7, 8, 9}
  1. Set Mathematical Intersection Operations

#input
set1={1,2,3,4,5,6}
set2={4,5,6,7,8,9}

## Intersection
intersection_set=set1.intersection(set2)
print(intersection_set)

#output
{4, 5, 6}
  1. Set Mathematical difference Operations

#input
set1={1,2,3,4,5,6}
set2={4,5,6,7,8,9}

#difference
print(set1.difference(set2))
print(set2.difference(set1))

#output
{1, 2, 3}
{7, 8, 9}
  1. Set Symmetric Difference Operations

#input
set1={1,2,3,4,5,6}
set2={4,5,6,7,8,9}

#Symmetric Difference
print(set1.symmetric_difference(set2))

#output
{1, 2, 3, 7, 8, 9}
  1. Set methods

#Input
set1={1,2,3,4,5}
set2={3,4,5}

## is subset
print(set1.issubset(set2))
print(set1.issuperset(set2))

#output
False
True

  1. Iterate over the set

#input
s = set(range(1, 6))
for elem in s:
    print(elem)

#output
1
2
3
4
5
  1. Creating the set using Range function

#input
s = set(range(1, 11))
print(s)

#output
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  1. Frozenset in python

  • A frozenset in Python is an immutable version of a set. Once created, its elements cannot be changed, added, or removed.

#input - creation
fs = frozenset([1, 2, 3])
print(fs)

#output
frozenset({1, 2, 3})

#frozenset using range function
fs = frozenset(range(1, 6))
print(fs)

#output
frozenset({1, 2, 3, 4, 5})
  1. set using star method

#input
list = [1,2,3,4,5,4,3,4]
my_list = [*set(list)]
print(mylist)

#output
[1, 2, 3, 4, 5]

Now, We have covered Set operations with various examples and methods. In the next article, we will dive into Dictionary operations in Python.