0% found this document useful (0 votes)
19 views14 pages

Python Sets: Definition and Operations

The document provides an overview of sets in Python, detailing their characteristics, creation methods, and operations. It explains how to traverse, access elements, and compare sets using various operators, along with built-in functions and methods applicable to sets. Additionally, it introduces the concept of frozen sets, which are immutable and useful for certain applications.

Uploaded by

nirmalseervi56
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

Python Sets: Definition and Operations

The document provides an overview of sets in Python, detailing their characteristics, creation methods, and operations. It explains how to traverse, access elements, and compare sets using various operators, along with built-in functions and methods applicable to sets. Additionally, it introduces the concept of frozen sets, which are immutable and useful for certain applications.

Uploaded by

nirmalseervi56
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

SUBJECT: PYTHON PROGRAMMING

TOPIC: SETS
SUBMITTED BY: Nirmal R (U18CM24S0004)
Deepak S N (U18CM24S0008)
Vineesh G (U18CM24S0037)

SEMESTER: 3rd Sem


SECTION: ‘A’ sec
COURSE: BCA
SUBMITTED TO: Prof. Leelavathi Mam
INTRODUCTION TO SETS
 A Set in python is an unordered collection of unique elements.
 The curly brackets {} is used to represent a set.
 The values in a set are called elements or items.
 Set is a mutable data type, thus, adding, removing or searching for items in
the set is possible.
 Sets are useful when you want to store unique items or perform
mathematical set operations such as: union and intersection.
 A Set is mutable, but set elements are immutable.
 Slicing operation cannot be performed on the set, because sets do not have
indexing.

Example: fruits={“apple”, “banana”, “mango”}


print(fruits) #Output: {‘apple’, ‘banana’, ‘mango’}
CREATING SETS
o To create a python set, place all elements or items separated by comma
inside curly braces{}.
o The built-in set() function can also be used to create a set in python.
o However, mutable elements such as: dictionaries, lists, or sets are not
allowed as its elements.
Syntax: set_name={val 1, val 2,…..val
n}
set_name=set()
Note: set_name=set(sequence)
o We need to use the set() function to create an empty set. Empty curly braces
will create an empty dictionary instead of an empty set.
Example: emptySet=set() # empty set
intSet={1,2,3} # set of integers
intFloatSet={10,20,13.75,100.5} # set of integers and float
colorSet={“red”, “green”} # set of strings..,etc.
TRAVERSING A SET
o A set is iterable. This means that we can use a for loop to traverse all the
elements in the set.
S={10,
Example: 20, 30, a“Nirmal”,
Traversing 39.99}
set using for loop. #Output: 20
for item in S:
39.99
print(item) 10

Nirmal

30

ACCESSING SET ELEMENTS


o Sets are mutable and unordered. Set data type does not support indexing and
slicing, so elements cannot be accessed using these two operations.
MEMBERSHIP OPERATORS
(in and not in)
o These operators are used to check if a particular element belongs to the set or
not. Member operators are of two types. They are:
1. ‘in’ Membership operator: It is used to check if a particular element is
present in the set or not. The output given by ‘in’ operator is same of boolean
operator. It gives output as either true or false.

Syntax:
element ‘in’ set

Example: mySet={10,20,30, “Python”, “Java”} # Output:


print(20 in mySet) True
print(“Java” in mySet) True
print(40 in mySet) False
2. ‘not in’ Membership Operator: This operator is used to
check if a particular element is not present in the set.
o The output given by ‘not in’ operator is also same of boolean
operator. It gives output as either true or false.
o The ‘not in’ always gives opposite output of ‘in’ operator i.e., If
‘not in’ gives False then ‘in’ operator will give True in output or
vice- versa.
Syntax: element ‘not in’ set

Example:
mySet={10,20, “Python”, “Java”} #Output:
print(20 not in mySet) False
print(40 not in mySet) True
COMPARING SETS
o A Set is a collection of unique and unordered elements.
Example: A={1, 3, 5}
B={1, 3, 5, 7}

o Python allows you to compare sets using relational operators like:


<, >, <=, >=, == & !=.
o These comparisons tell us how sets relate to each other- for
example, whether one is a subset or superset of another.
o Subset: If set A is contained in set B, then A is said to be the subset
of B.
o Superset: if B is a superset of A, then all elements of A are in B.
COMPARISON OPERATORS:

1. Equal to(==): Returns A={1,


true if both
3, 5}the sets contain exactly the same elements.
Example: B={5, 1, 3}
print(A==B)
#True
2. Not Equal(!=): Returns true if sets are not equal, i.e., they contain different
A={1, 3, 5}
elements. Example: B={2, 6, 7}
print(A!=B)
#True
3. Less than(<): Returns true if the first set is a proper subset of the second set.
That means all the elements ofA={1,
A are in3,B,5}but B has extra elements.
Example: B={1, ,3, 5, 7}
print(A<B)
#True
4. Greater than(>): Returns true if the first set is a proper superset of the second
set. That means A contains all the elements of B and some extra.
Example: A={1 ,3, 5, 7}
B={1, 3, 5}
print(A>B)
#True
5. Less the=an or equal to(<=): Returns true if A is a subset of B. That means all
elements of A are in B. This can include equal sets also.
Example: A={1, 3, 5}
B={1, 3, 5, 7}
print(A<=B) #True
C={1, 3, 5}
print(A<=C) #True
(equal sets)

6. Greater than or equal to(>=): Returns true if A is a superset of B. That means


A contains all elements of B. Equal sets also return True.
Example:
A={1, 3, 5 ,7}
B={1, 3, 5}
print(A>=B) #True
C={1, 3, 5, 7}
Print(A>=C) #True (equal
sets)
BUILT-IN FUNCTIONS USED ON SETS
[Link](): Finds how many unique elements are in a set. Duplicates are not counted
in sets. Syntax: len(set_name) Example: S1={4,3,5,2,4,5
}
print(len(S1))
OUTPUT: 4
[Link](): Adds all numeric elements in a set. Works only for numbers; gives an
error for strings or mixed data. Syntax: sum(set_name)
Example: S2={10,20,30,40,
50}
print(sum(S2))
OUTPUT: 150
3. max(): Finds the largest element in a set.
S3={34,76,89,33,54,
Syntax: max(set_name) Example: 65}
print(max(S3)
OUTPUT: 89
4. min(): Finds the smallest element in a set.
Syntax: min(set_name) Example: S4={45,28,56,43
}
print(min(S4))
OUTPUT: 28
SET METHODS
1. add(): Adds an element to the set. If the element already exists, nothing happens.
Syntax: set_name.add(element) Example: S={10, 20, 30,
40}
[Link](50)
2. clear(): Removes all items from the set(makes it print(S)
Empty). Syntax: set_name.clear() Example: OUTPUT:
S={10,30,40} {10,20,30,40,50}
[Link]()

3. copy(): Creates a shallow copy (duplicate) of a set.


Syntax: set_name.copy() Example: S1={10,20,30}
S2=[Link]()
print(S2)
OUTPUT:
{10,20,30}
4. difference(): Returns elements that are in one set, but not in another.
Syntax: [Link](set2) Example: S1={1,2,3,4,5}
S2={4,5,6,7,8}
print([Link](B)
)
OUTPUT: {1,2,3}
5. discard(): Removes a specific element from the set. If the
element is not found, no error occurs. Syntax:
set_name.discard(element)langs={‘Python’, ‘C++’, ‘Java’,
Example: ‘Go’ }
[Link](‘C++’)
print(langs)
OUTPUT: {‘Python’, ‘Java’, ‘Go’}

6. difference_update(): Removes elements from the first set that are


also in the second set. Syntax: set1.difference_update(set2)
Example: A={1,2,3,4,5}
B={1,2,3}
A.difference_update(B)
print(A)
OUTPUT: {4,5}
FROZEN SET
o A Frozen set is just like a normal set, but it is immutable (cannot be changed
after creation). That means once you create it, you can’t add or remove elements.
o It is useful when you want a set that should never change, for example, to use it
as a dictionary key or a set inside another set.
Syntax: frozenset([iterable])
• Here, iterable can be a list, tuple, or dictionary and if nothing is given, it creates
an empty frozenset.
set1={10,20,30,40,50}
Example1:Creating Frozen set fset=frozenset(set1)
print(“The frozen set is:”,fser=t)
OUTPUT: The frozen set is:
frozenset({50,20,40,10,30})
Example2: Using Frozen set with a Dictionary
Student={“name”: “Nirmal”, “age”: 21, “ID”:1001}
key=frozenset(Student)
print(“The frozen set is:”,key)
OUTPUT: The frozen set is : frozenset({‘age’, ‘name’, ‘ID})

• Here only the keys of the dictionary are converted into a frozen set.
ANY QUERIES?

THANK YOU

You might also like