SETS IN PYTHON
SUMMER INTERNSHIP - I
Under Supervision RUCHI JAT & ALKA THAKRE
Mr. Mahesh Dhakad DEPARTMENT OF CHEMICAL ENGINEERING
GOVT. POLYTECHNIC COLLEGE RAJGARH (M.P.)
TABLE OF CONTENT
INTRODUCTION TO SET
CHRACTERSTICS OF SET
CREATING A SET
ACCESSING ELEMENT IN A SET
ADDING AND REMOVING ELEMENTS
SET OPREATIONS
APPLICATION OF SET IN PYTHON
CONCLUSION
PYTHON
Python language was created by “Guido van Rossum” in 1991.
The name python was came from a TV show named “Monty Python’s Flying Circus”.
Python is high level interpreted general purpose programming language, derived from ABC
PROGRAMMING LAUNGUAGE.
It is object oriented programming language.
It is easy to learn.
INTRODUCTION TO SET:-
• A Set is an unordered collection of unique element.
• Duplicate values are not allowed.
• Defined using { } curly brackets.
•Example:
myset ={1, 2,3,4}
CHARACTERISTICS OF SET
Unordered (no indexing)
Only unique elements
Mutable (can be modified)
Can store heterogeneous data types (int,str,float,etc.)
CREATING A SET
Methods:
1. Using curly braces{ }
S = {1,2,3}
2. Using set() function
S = set([1,2,2,3])
ACCESSING ELEMENTS IN A SET
Indexing is not possible.
Elements can be accessed using a loop:
For I in myset:
Print(I)
AddING AND REMOVING ELEMENTS
Add element: [Link](x)
Remove element: [Link](x) or [Link](x)
Clear all elements: [Link]()
SET OPERATIONS
Union (| or union() )
Intersection (& or intersection() )
Difference (- or difference() )
Symmetric Difference (^ or symmetric_difference() )
EXAMPLES OF SET OPERATIONS
A={1,2,3,4}
B={3,4,5,6}
Print(A|B) # Union
Print(A&B) # Intersection
Print(A-B) # Difference
Set union
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use | operator
# output: {1, 2, 3, 4, 5, 6, 7, 8}
print(A | B)
# use union function # use union function
[Link](B) [Link](A)
{1, 2, 3, 4, 5, 6, 7, 8} {1, 2, 3, 4, 5, 6, 7, 8}
Set intersection
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use & operator
# output: {4, 5}
print(A & B)
A. intersection(B)
# {4, 5}
Set difference
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use – operator on A
# output: {1, 2, 3}
print(A – B)
# use difference function on A
[Link](B) # use – operator on B
#{1, 2, 3} B–A
# {8, 6, 7}
Set symmetric difference
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
# use ^ operator
# Output: {1, 2, 3, 6, 7, 8}
print(A ^ B)
# use symmetric difference function on A
[Link] difference(B)
# {1, 2, 3, 6, 7, 8}
APPLICATIONS OF SET IN PYTHON
Removing duplicate values
Membership texting
Solving mathematical problems
Used in data science and machine learning
CONCLUSION
Set is a powerful data structure.
Best for fast searching, duplicate removal and set orerations.
Very useful in data handling and programming.
THANK YOU