Click for All Topics
{}
(curly braces).Example – creating a set in Python
# Example 1-
s={1,2,3,(10,11,12),'anurag','edSlash'}
print(s)
print(type(s))
# Output
# {1,2,3,(10,11,12),'anurag','edSlash'}
#
# Example 2-
st={1,2,2,3,4,4,4} # it will remove the extra 2,4 from set
print(st)
# Output
# {1,2,3,4}
# Example 3 -
t={'hey','Python',[12,13,17]} # Will give an error
print(t)
# Error -
# Traceback (most recent call last):
# File "/home/main.py", line 1, in
# t={'hey','Python',[12,13,17]} # Will give an error
# TypeError: unhashable type: 'list'
Creating a set using set method –
set()
which is also used to create a set.Example
sets=set([1,2,3,4])
print(sets)
print(type(sets))
# Output
# {1,2,3,4}
#
# Create an empty set
n=set()
print(n)
print(type(n))
# Output
# set()
#
.add()
method.Example –
# Example 1-
s={1,2,3,4}
print(s)
s.add(5) #adding 5 in set(s)
print(s)
# Output -
# {1,2,3,4}
# {1,2,3,4,5}
# Example 2-
s={'hello','Everyone'}
print(s)
s.add('edSlash')
print(s)
# Output -
# {'hello','Everyone'}
# {'hello','Everyone','edSlash'}
.update()
method.Example – let’s see an example
st={'hello','Python','set'}
print(st)
l=['This','is','edSlash','Official']
st.update(l)
print(st)
# Output -
# {'hello','Python','set'}
# {'hello','Python','set','This','is' 'edSlash','Official'}
.discard()
method – Example –
s={10,20,30,40}
print(s)
s.discard(30)
print(s)
# Output -
# {10,20,30,40} #original set
# {10,20,40} # after deleting 30 from set
.remove()
method –
Example –
# Example 1 -
s={10,20,23,(1,2,3)}
print(s)
s.remove((1,2,3))
print(s)
# Output -
# {10,20,23,(1,2,3)} # original set
# {10,20,23} # after removing tuple from set(s)
# Example 2 -
s={'Mr.','Anurag','Dubey'}
print(s)
s.remove('edSlash') # Will raise an error
print(s)
# Output -
# {'Mr.','Anurag','Dubey'}
# Traceback (most recent call last):
# File "/home/main.py", line 3, in
# s.remove('edSlash') #Will raise an error
# KeyError: 'edSlash'
.clear()
.Example –
s={11,22,33,44,55}
print(s)
s.clear()
print(s)
# Output -
# {11,22,33,44,55} # original set
# set() # set after deleting all elements
Delete set from memory –
del
keywordExample – Let’s see an example
s={1,2,3,4}
print(s)
del s
print(s) # will give an error
# Output -
# {1,2,3,4}
# Traceback (most recent call last):
# File "/home/main.py", line 4, in
# print(s)
# NameError: name 's' is not defined
Example – let’s see an example
s={10,20,30,40}
for i in s:
print(i)
# Output -
# 40
# 10
# 20
# 30
.union()
method and |
operator are used to perform union operation on two sets.Example – let’s see an example
# Example 1 -
a={1,2,3}
b={3,4,5}
c=a.union(b)
print(c)
# Output -
# {1,2,3,4,5}
# Example 2 -
s1={'a','b','c','d'}
s2={'c','d','e','f','g'}
s=s1|s2
print(s)
# Output -
# {'a','b','c','d','e','f','g'}
&
operator and .intersection
method is used to perform intersection operations between sets.Example –
# Example 1-
set1={1,2,3,4}
set2={3,4,5,6}
final_set=set1&set2
print(final_set)
# Output -
# {3, 4}
# Example 2-
s1={'a','b','c','d'}
s2={'c','d','e'}
s3={'c','b','d'}
s=s1.intersection(s2,s3)
print(s)
# Output -
# {'c', 'd'}
-
and .difference
is used to perform difference operations on sets.Example –
# Example 1 -
s1={1,2,3,4}
s2={4,5,6,7,8}
s=s1-s2
print(s)
# Output -
# {1, 2, 3}
# Example 2 -
s1={'a','b','c','d'}
s2={'c','d','e'}
s3={'c','b','d'}
s=s1.difference(s2,s3)
print(s)
# Output -
# {'a'}
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India