Sets and its Methods in Python

Hey Guys,
In this python tutorial, you are going to learn about Sets, it i.e definition, creation and its methods.
In Python Set is a data structure is equivalent to sets in mathematics.

Python has a few methods that set objects can all to perform frequency occurring task. All those methods are discussed here.

Sets and its Methods

A set is a collection which is unordered and unindexed.

You can define a set simply just by naming all of its elements inside brackets. The only exception is the empty set, that can be created by using the method set(). If set(..) has a list, a string or a tuple as a parameter, it will return a set composed of its elements.

 A set cannot have mutable elements like a list, set or dictionary, as its elements.

CREATION OF SET

In Python, sets are written with curly brackets[{}] separated by commas.

x={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"}

z={"25","30","35","40"}

w={"odd","even","prime"}

print(x)

print(z)

print(w)
OUTPUT:

{'Sat', 'Fri', 'Tue', 'Sun', 'Wed', 'Thu', 'Mon'}
{'40', '30', '35', '25'}
{'prime', 'even', 'odd'}

Python Set Methods

Here all methods of set objects and built-in functions are listed that can take set as a parameter and perform some task.



Python Set remove() : Removes Element from the Set
We can remove elements from a set by using the discard() method. Again as we have just discussed that there is no specific index attached to the newly added element in our set.
Syntax:
set.remove(element)

Python Set add() : adds element to a set

We can add elements to a set by using add() method. Remember that, there is no specific index attached.

 Syntax:
set.add(elem)
Python Set copy(): Returns Shallow Copy of a Set

Syntax:

numbers = {1, 2, 3, 4}

new_numbers = numbers
Python Set clear(): remove all elements from a set

Syntax:

set.clear()

Python Set difference() : Returns Difference of Two Sets

Syntax:

A.difference(B)
Python Set difference_update() : Updates Calling Set With Intersection of Sets
Syntax:
A.difference_update(B)
Python Set discard() : Removes an Element from The Set.
Syntax:
s.discard(x)
Python Set intersection() : Returns Intersection of Two or More Sets
Syntax:
A.intersection(*other_sets)
Python Set intersection_update() : Updates Calling Set With Intersection of Sets
Syntax:
A.intersection_update(*other_sets)
Python Set isdisjoint(): Checks Disjoint Sets
Syntax:
set_a.isdisjoint(set_b)
Python Set issubset(): Checks if a Set is Subset of Another Set
Syntax:
A.issubset(B)
Python Set issuperset(): Checks if a Set is Superset of Another Set
Syntax:
A.issuperset(B)
Python Set pop() : Removes an Arbitrary Element
Syntax:
set.pop()
Python Set symmetric_difference() : Returns Symmetric Difference
Syntax:
A.symmetric_difference(B)
Python Set symmetric_difference_update() : Updates Set With Symmetric Difference
Syntax:
A.symmetric_difference_update(B)
Python Set union() : Returns Union of Sets
The union operation on two sets produces a new set containing all the distinct elements from both the sets. In the below example the element “Wed” is present in both the sets.
Syntax:
A.union(*other_sets)
Python Set update() : Add Elements to The Set.
Syntax:
A.update(B)
Python any(): Checks if any Element of an Iterable is True
Syntax:
any(iterable)
Python all(): returns true when all elements in iterable are true
Syntax:
all(iterable)
Python ascii(): Returns String Containing Printable Representation.
Syntax:
ascii(object)
Python bool(): Converts a Value to Boolean.
Syntax:
bool([value])
Python enumerate() : Returns an Enumerate Object
Syntax:
enumerate(iterable, start=0)
Python filter(): constructs iterator from elements which are true
Syntax:
filter(function, iterable)
Python frozenset() : returns immutable frozenset object.
Syntax:
frozenset([iterable])
Python iter() : returns iterator for an object.
Syntax:
iter(object[, sentinel])
Python len()  : Returns Length of an Object.
Syntax:
len(s)
Python max() : returns largest element.
Syntax:
max(iterable, *iterables[,key, default])
max(arg1, arg2, *args[, key])
Python min() : returns smallest element.
Syntax:
min(iterable, *iterables[,key, default])
min(arg1, arg2, *args[, key])
Python map() :  Applies Function and Returns a List.
Syntax:
map(function, iterable, ...)
Python set() : returns a Python set.
Syntax:
set([iterable])
Python sorted() : returns sorted list from a given iterable.
Syntax:
sorted(iterable[, key][, reverse])
Python sum() : Add items of an Iterable.
Syntax:
sum(iterable, start)
Python zip() : Returns an Iterator of Tuples.

Syntax:

zip(*iterables)
 EXAMPLE PROGRAM
x={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"}

y={"Jan","Feb","Mar","Wed"}

x.discard("Sun")     #remove

print(x)

x.add("Sun")    #add

print(x)

z = x|y       #union

print(z)

w = x & y  #intersection

print(w)

a = x- y    #difference

print(a)

b= x <= y      #compare

c = y >= x

print(b)

print(c)

OUTPUT:

{'Tue', 'Sat', 'Wed', 'Mon', 'Fri', 'Thu'}
{'Tue', 'Sat', 'Wed', 'Mon', 'Fri', 'Thu', 'Sun'}
{'Jan', 'Tue', 'Feb', 'Sat', 'Wed', 'Mon', 'Mar', 'Fri', 'Thu', 'Sun'}
{'Wed'}
{'Tue', 'Sat', 'Mon', 'Fri', 'Thu', 'Sun'}
False
False
Also, read:

 

Leave a Reply

Your email address will not be published. Required fields are marked *