Set
- In python,a set is a type of collection in data type.
- A set is a mutable(changeable),unordered data. It doesn't allow duplicate values.
- Sets are useful in doing set operation.
- The provided with operations like,intersection,union, difference and symmetric.
- Items in set are enclosed by curly brackets { }.
- Each item is separated by comma (,).
Syntax:
Creating A Set
- A set is created by placing all the elements separated by comma within a pair of curly brackets.
- Since sets are unordered and unindexed ,you cannot be sure in which the items can be integer ,float, string or tuple.
- It allows the heterogeneous (different type) type of values in one set.
- It automatically eliminates the duplicate items. The set( ) function can also be used to create sets in python.
Syntax:
Set Methods In Python
The in-built methods of set are used to:
- create
- insert
- remove
- delete the set
- set items
Example:
Using all method program in set
Program:
# create an empty set
my_set = set()
# add items to the set
my_set.add('apple')
my_set.add('banana')
my_set.add('orange')
# print the set
print('Set after adding items:', my_set)
# remove an item from the set
my_set.remove('banana')
# print the set
print('Set after removing an item:', my_set)
# delete the entire set
del my_set
# try to print the set (will result in a NameError)
print(my_set)
Output:
Set after removing an item: {'orange', 'apple'}
Traceback (most recent call last):
File "set_program.py", line 16, in <module>
print(my_set)
NameError: name 'my_set' is not defined
Set Operations
- The set class supports mathematical set operations like union,intersection,difference and symmetric.
- These operations can be performed either using in-built functions or using operators.
- The operators work only on onset data structure.
- The function works on any iterable data structure like list and tuple.
1. Union Operation
Syntax:
Program:
# define sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# perform the union operation using the | operator
union_set = set1 | set2
print(union_set)
Output:
{1, 2, 3, 4, 5, 6, 7}
2. Union Function
Syntax:
Program:
# define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# perform the union operation using the union() method
union_set = set1.union(set2)
print(union_set)
Output:
{1, 2, 3, 4, 5, 6, 7}
3. Intersection Operation
The intersection operation is to get the elements commonly available in two sets The intersection operation can be done using the logical 'and' (&) operator.
Syntax:
Program:
# define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# perform the intersection operation using the & operator
intersection_set = set1 & set2
print(intersection_set)
Output:
{3, 4, 5}
4. Intersection Function
Syntax:
Program:
# define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# perform the intersection operation using the intersection() method
intersection_set = set1.intersection(set2)
print(intersection_set)
Output:
5. Difference Operations
Syntax:
NewSer = Set1 - Set2
Program:
# define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# perform the difference operation using the - operator
diff_set = set1 - set2
print(diff_set)
Output:
6. Difference Function
Syntax:
Program:
# define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# perform the difference operation using the difference() method
diff_set = set1.difference(set2)
print(diff_set)
Output:
{1, 2}
7. Symmetric Difference Operation
The symmetric difference operation is to get the elements of two sets after excluding the common elements .This operation can be performed using the exponential operator.
Syntax:
newset= set1 ^ set2
Program:
# define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# perform the symmetric difference operation using the ^ operator
sym_diff_set = set1 ^ set2
print(sym_diff_set)
Output:
{1, 2, 6, 7}
More topic in Python