0% found this document useful (0 votes)
35 views25 pages

Setmethodsinpython 200406125517

Sets are unordered collections of unique elements. Elements cannot be duplicated and must be immutable. Sets can perform operations like union, intersection, difference and symmetric difference. A set is created using curly brackets or the set() function. Common set methods allow you to add and remove elements, check for subset/superset relationships, and perform mathematical set operations on multiple sets.

Uploaded by

z9819898203
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)
35 views25 pages

Setmethodsinpython 200406125517

Sets are unordered collections of unique elements. Elements cannot be duplicated and must be immutable. Sets can perform operations like union, intersection, difference and symmetric difference. A set is created using curly brackets or the set() function. Common set methods allow you to add and remove elements, check for subset/superset relationships, and perform mathematical set operations on multiple sets.

Uploaded by

z9819898203
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/ 25

SET

SET

• A set is an unordered collection of items. Every element is unique (no


duplicates) and must be immutable (which cannot be changed).

• However, the set itself is mutable. We can add or remove items from it.
• Sets can be used to perform mathematical set operations like union,
intersection, symmetric difference etc.
Creating SET
• A set is a collection which is unordered and unindexed.
• A set iss created by placing all the items(elements) inside with curly
brackets{},separated by comma or by using the built-in function set().
• It can have any number of items and they may be of different types (integer, float, tuple,
string etc.).
• Empty curly braces {} will make an empty dictionary in Python.
• To make a set without any elements we use the set() function without any argument.
Creating SET
• Important : But a set cannot have a mutable element, like list, set or dictionary, as its
element.

Example :
set_four={1,2,[3,4]}
print set_four
#output : set_four={1,2,[3,4]}
TypeError: unhashable type: 'list’Because Set is immutable
and list is mutable so we can’t contain list in set.
Creating SET

• We can make set from a list.

my_set = set([1,2,3,2])
print(my_set)
#Output :
{1, 2, 3}
Access Items
• You cannot access items in a set by referring to an index, since sets are unordered the
items has no index.
• But you can loop through the set items using a for loop, or ask if a specified value is
present in a set, by using the in keyword.
thisset = {“sad", “happy", “love“,”hate”}
for x in thisset:
print(x)
# Output :
sad
happy
love
hate
Change SET methods
• To add one item to a set use the add( ) method.
• To add more than one item to a set use the update() method.
• The update() method can take tuples, lists, strings or other sets as its
argument. In all cases, duplicates are avoided.
Remove element from SET
• A particular item can be removed from set using methods, discard() and remove().
• The only difference between the two is that, while using discard() if the item does not
exist in the set, it remains unchanged. But remove() will raise an error in such condition.
• You can also use the pop(), method to remove an item, but this method will remove the
last item. Remember that sets are unordered, so you will not know what item that gets
removed. The return value of the pop() method is the removed item.
• The clear() method empties the set.
• The del keyword will delete the set completely.
Mathematical Set Operations
Union( )
• Union operation is performed using | operator. Same can be accomplished using the
method union(). Union of sets is a set of all elements from both A & B sets.

# set operations
A = {1, 3, 5, 7, 9}
B = {2, 4, 6,8,10}
print(A|B)
print(A.union(B))
print(B.union(A))
#Above all print statement output will be same.
#Output :{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Intersection( )
• Intersection operation is performed using & operator. Same can be accomplished using
the method intersection(). Intersection of sets is a set of elements that are common in
both sets.
# set operations
A = {1, 3, 4, 7, 8}
B = {2, 4, 3,8,10}
print(A&B)
print(A.intersection(B))
print(B.intersection(A))
#Above all print statement output will be same.
#Output :{3,4,8}
Difference( )
• Difference is performed using - operator. Same can be accomplished using the method
difference(). Difference of A and B (A - B) is a set of elements that are only in A but not
in B. Similarly, B - A is a set of element in B but not in A.
# set operations
A = {1, 3, 4, 7, 8}
B = {2, 4, 3,8,10}
print(A-B)
print(A. difference(B))
#output :{1, 7}
print(B-A)
print(B.difference(A))
#output :{2,10}
symmetric_difference( )
• Symmetric Difference of A and B is a set of elements in both A and B except those that
are common in both. Symmetric difference is performed using ^ operator. Same can be
accomplished using the method symmetric_difference().
# set operations
A = {1, 3, 4, 7, 8}
B = {2, 4, 3,8,10}
print(A^B)
print(A. symmetric_difference(B))
print(B^A)
print(B. symmetric_difference (A))
#Above all print statement output will be same.
#output :{1,2,7,10}
Copy( )
• The copy() method copies the set.

fruits = {"apple", "banana", "cherry"}

x = fruits.copy()

print(x)
difference_update( )
• The difference_update() method removes the items that exist in both sets.
• The difference_update() method is different from the difference() method, because the
difference() method returns a new set, without the unwanted items, and the
difference_update() method removes the unwanted items from the original set.
• Syntax :
• A.difference_update(B)
• Here, A and B are two sets. The difference_update() updates set A with the set
difference of A-B.
• The difference_update() returns None indicating the object (set) is mutated.
difference_update( )
• Example:
A = {'a', 'c', 'g', 'd'} A = {'a', 'c', 'g', 'd'}
B = {'c', 'f', 'g'} B = {'c', 'f', 'g'}
result = A.difference_update(B) result = B.difference_update(A)
print('A = ', A) print('A = ', A)
print('B = ', B) print('B = ', B)
print('result = ', result) print('result = ', result)
#output: ( 'A = ', set(['a', 'd'])) #output: ('A = ', set(['a', 'c', 'd', 'g']))
('B = ', set(['c', 'g', 'f'])) ('B = ', set(['f']))
('result = ', None) ('result = ', None)
symmentric_difference_update( )
• The symmetric_difference_update() method finds the symmetric
difference of two sets and updates the set calling it.
• Syntax :
• A.symmetric_difference_update(B)
• The symmetric_difference_update() returns None (returns nothing).
Rather, it updates the set calling it.
symmentric_difference_update( )

Example:
A = {'a', 'c', 'd'}
B = {'c', 'd', 'e’ } Output :
result=A.symmetric_difference_update(B) A = {'a', 'e'}
print('A =', A) B = {'d', 'c', 'e'}
print('B =', B) result = None
print('result =', result)
This method returns None (meaning, absence of a return value). It only updates the set calling the intersection_update() method.

intersection_update( )
• The intersection of two or more sets is the set of elements which are
common to all sets.
• Syntax:
• A.intersection_update(*other_sets)
• The intersection_update() allows arbitrary number of arguments (sets).
• This method returns None (meaning, absence of a return value). It only
updates the set calling the intersection_update() method.
intersection_update( )
Example :
A = {1, 2, 3, 4}
B = {2, 3, 4, 5, 6}
C = {4, 5, 6, 9, 10}
result = C.intersection_update(B, A)
print('result =', result)
print('C =', C)
print('B =', B)
print('A =', A)
#Output :
result = None
C = {4}
B = {2, 3, 4, 5, 6}
A = {1, 2, 3, 4}
issubset( )
• The issubset( ) method returns True if all elements of a set are present in another
set (passed as an argument). If not, it returns False.
• Set A is said to be the subset of set B if all elements of A are in B.
• Syntax:
• A.issubset(B)
• The issubset() returns
✓ True if A is a subset of B
✓ False if A is not a subset of B
issuperset( )
• The issuperset() method returns True if all items in the specified set exists in the original set,
otherwise it retuns False.
• Syntax:
• A.issuperset(B)
• The issuperset() returns
✓ True if A is a superset of B
# Return True if all items in set B are present in set A.
✓ False if A is not a superset of B
# Return False if not all items in set B are present in set A.
issubset( )
• Example :
A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
C = {1, 2, 4, 5} Output:
print(A.issubset(B)) True
print(B.issubset(A)) False
print(A.issubset(C)) False
print(C.issubset(B)) True
disjoint( )
• The isdisjoint() method returns True if two sets are disjoint sets. If not, it returns False.
• Two sets are said to be disjoint sets if they have no common elements.
• Syntax:
• A.disjoint( )
• The isdisjoint() method returns

✓ True if two sets are disjoint sets


✓ (if set_a and set_b are disjoint sets in above syntax)
✓ False if two sets are not disjoint sets
len( ),set( )
• To determine how many items a set has, use the len() method.
• Syntax: len(set_name)
• It is also possible to use the set() constructor to make a set.
• Example:
thisset = set(("apple", "banana", "cherry"))
# note the double round-brackets
print(thisset)

You might also like