0% found this document useful (0 votes)
11 views2 pages

5) Set

The document provides an overview of the set data type in programming, detailing its characteristics such as being unordered, mutable, and eliminating duplicate values. It outlines various functions associated with sets, including adding, removing, updating, and performing set operations like union and intersection. Additionally, it emphasizes the immutability of set elements and the inability to access them via indexing.

Uploaded by

Krishna P
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)
11 views2 pages

5) Set

The document provides an overview of the set data type in programming, detailing its characteristics such as being unordered, mutable, and eliminating duplicate values. It outlines various functions associated with sets, including adding, removing, updating, and performing set operations like union and intersection. Additionally, it emphasizes the immutability of set elements and the inability to access them via indexing.

Uploaded by

Krishna P
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/ 2

Set

16 October 2023 16:23

Set data type:


Set is a collection of homogenous data item or heterogeneous data item which is enclosed between the pair of flower brackets {}.
Syntax:
• The values should be immutable i.e.,(int, float, complex, string, tuple).
• Set is unordered in nature (the values will not be stored in a same order like
how the user have given).
• In set duplicated values will get eliminated.
• Indexing is not possible in set collection because of its unordered nature.
a = {1, 2, 3, 3, 4}
print(len(a))

Memory allocation in set data type:


Same as list and tuple

• It is not possible to access values from set using indexing and not possible to modify values based on indexing.

Attributes on set data type:


• Add (): It is a function which is used to add a new value to the existing set collection.
Variable.add(value)
Ex : a = {'apple', 'yahoo', 'google'}
a.add('facebook')

• Remove (): It is a function which is used to remove an existing value from the given set collection.
Variable.remove(value)
Ex : a.remove("apple")

• Update (): It update the current set by adding items from any iterable.
Variable.update(iterable)
Ex : a = {1, 2}
b = {3, 4}
a.update(b) # a = {1, 2, 3, 4}

• Discard (): It is a function which is used to remove specified item from the set.
Variable.discard(item)
Ex : a.discard("apple")

• Pop (): It is a function which is used to remove random item from the set. It will return removed item.
Variable.pop()
Ex : a.pop()

• Clear (): It is a function which is used to remove the all the item from the set.
Variable.clear()
Ex: a.clear()

• Isdisjoint (): It returns True if none of the item present in both the set or else False.
Base_set.isdisjoint(sub-set)
x = {1, 2, 3}
y = {2, 3, 5}
x.isdisjoint(y) # Returns False

x1 = {1, 3, 5}
x2 = {2, 4, 6}
x1.isdisjoint(x2)

• Issubset (): It returns True if all the item from the base set exists in the reference set, or else False.
Base_set.issubset(sub-set)
Ex : a = {1}
b = {1, 2}
c = {1, 2, 3}
d = {1, 2, 4}

a.issubset(b) # Returns True

New Section 2 Page 1


a.issubset(b) # Returns True
b.issubset(c) # Returns True
c.issubset(d) # Returns False

• Issuperset (): It returns True if all the items in the reference set exist in the base set.
Base_set.issubset(sub-set)
• Union (): It returns a set that contains all the items from the original set and all items from specified set.
Set1.union(set2)
Ex : a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
a.union(b) # Prints {1, 2, 3, 4, 5, 6}

c = {6, 7, 8, 9}
a.union(b, c) # Prints {1, 2, 3, 4, 5, 6, 7, 8, 9}

• Intersection (): It returns set that contains the similarity between two or more sets.
Set1.intersection(set2)
Ex : a.intersection(b) # Returns a new set {3, 4}

• Difference (): It returns a set containing the difference between two or more sets. The retuned set contains items that exist only in
base set.
-base set: the first set which we write before applying function.
-reference set /subset: the set which is written inside the round brackets.
Set1.differnce(set2)
Ex: a.difference(b)
# a - b # Prints {1, 2}
a = {1, 2, 3, 30, 300}
b = {10, 20, 30, 40}
c = {100, 200, 300, 400}
a.difference(b, c) # Returns a new set {1, 2, 3}

• Symmetric_difference (): It returns a set which contains all the items which are not common among all the set.
Set1. Symmetric_difference (set2)

• Intersection_update (): It modifies the set by retaining the only elements found in both set.
Set1. Intersection_update(set2)
Ex: a = {1, 2, 3}
b = {2, 3, 4}
a.intersection_update(b) # Prints {2, 3}

• Difference_update (): It update the base set with the elements that are present in base set but not in other set.
Set1.difference_update(set2)
Ex : a = {1, 2, 3}
b = {2}
a.difference_update(b) # Prints {1, 3}

• Symmetric_difference_update (): It updates the base items that are not common among all the sets (both the set)
Set1. Symmetric_difference_update (set2)
Note:
• We can modify the set collection so it is a mutable data type.
• Since set will not remove duplicate values, it is used in “data filtering process”.

New Section 2 Page 2

You might also like