0% found this document useful (0 votes)
1 views

SETS Examples

The document provides various examples demonstrating the use of sets in Python, including creating sets, adding elements, removing duplicates, and performing set operations like union, intersection, and difference. It also highlights the properties of sets, such as their unordered nature and lack of indexing support. Additionally, it includes examples of set comprehensions and checks for membership within sets.

Uploaded by

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

SETS Examples

The document provides various examples demonstrating the use of sets in Python, including creating sets, adding elements, removing duplicates, and performing set operations like union, intersection, and difference. It also highlights the properties of sets, such as their unordered nature and lack of indexing support. Additionally, it includes examples of set comprehensions and checks for membership within sets.

Uploaded by

dreamerelonmusk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

SETS Examples

Q1.

s={1, 2, 3, 4, 5, 1, 2, 3, 4, 5}
print(s)
print(type(s))

#
s=[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(s)
print(type(s))

#
s={}
print(s)
print(type(s))

#
s=set()
print(s)
print(type(s))
#
s = set("Hello everyone")
print(s)
#
s = set("Hello everyone", "Hi")

print(s)

Q2.

l1=[1,2,3,4,5]
print(list(set(l1)))

#
s = set([1,2,3,4,5,6])
print(s)

Q3.

#### Sets does not support indexing

# Defining a set
set1={2,3,4,5}
# sets does not support indexing
# Below command will throw error!
set1[2]

Q4.

set1={2,2,3,1,1,4,4}
set2={5,5,3,4,5,5}
set3={'a','c','c','b','b'}

print(type(set1))

print(set1)
print(set2)
print(set3)
Q5.

sampleList=[2,3,4,4,4,5,5,5,8,2,'b','b']
sampleTuple=(2,3,'a','a',4,4,10)

# Removing duplicates using set


print(set(sampleList))
print(set(sampleTuple))

Q6.
S = {"AA", "AB", "AC"}
print(S)

S.add("AD")
print(S)

Q7.

s = {2,3,4,5}
s.add(4)
s.add(6)
s.add(7)
s.add(1)
print(s)

Q8.
s = {20,30,40,50}
tupl = (120, 130, 150)
s.add(tupl)
print(s)

Q9.
s = {6,7,8,9}
s.update({5,3,2})
print(s)

Q10.
s = {"sa","sb","sc"}
print(s)
s.update("1", "2","3")
print(s)

Q11.

items1 = {1, 2, 3}
items2 = {1, 2, 3}
word = 'datagy'

items1.add(word)
items2.update(word)

print('items1 = ', items1)


print('items2 = ', items2)

Q12.

S = {"BB","CC", "DD", "FV"}


print(S)
S.remove("FV")
print(S)
S.discard("BB")
print(S)

Q13.

S1 = {2,3,4,5}
S2 = {"One", "Two", "Three"}

s = S1.union(S2)
print(s)

S1 = {20,13,41,5}
S2 = {"Ten", "Four", "Three"}

s = S1.union(S2)
print(s)

Q14.

S1 = {1,2,3}
S2 = {2,3,4}

s = S1.union(S2)
print(s)
Q15.
S1 = {1,2,3}
S2 = {2,3,4}
print("Numbers:", S1.union(S2))
print("Numbers:", S1|S2)

Q16.

chocolates = {'munch','kitkat','diarymilk'}
fruits = {'mango','orange','pineapple'}
vegetables = {'tomato','chilli','potato','peas'}
ice_cream = {'vanilla','candy','blackcurrent'}
chocolates_fruits =
chocolates.union(fruits,vegetables,ice_cream)
print(chocolates_fruits)

Q17.
items = [{'chocolate','biscuits'}, {'potato','tomato'},
{'apple','orange','pear'}]
print(set().union(*items))
#The list is unpacked by using asterisk *
Q18.
Numbers = [{2,4,6}, {1,3,5}, {0}, {3,6,9},{4,8,12}]
print(set().union(*Numbers))

Q19.
Even_number = {2,4,6,8}
Odd_number = {1,3,5,7}
Multiple_of_3 = {3,6,9,12}
Multiple_of_4 = {4,8,12,16}
Numbers =
Even_number.union(Odd_number,Multiple_of_3,Mul
tiple_of_4)
print(Numbers)

Q20.
Numbers = [set([2,4,6,8]), set([1,3,5,7]), set([0])]
set = frozenset().union(*Numbers)
print(set)
#The frozenset() is a function that returns all the set
objects from the list. An asterisk * is used to unpack
the list.

Q21.
list1 = [2,4,6,8,10]
list2 = [1,2,3,4,5,7]
result =(set().union(list1,list2))
print(result)
Q22.

m1 = {"Samsung","Apple","OnePlus"}
m2 = {"Oppo","Apple","Vivo"}
i = m1.intersection(m2)
print(i)

Q23.
s1 = {"c", "y", "z"}
s2 = {"a", "c", "e"}
s3 = {"f", "g", "c"}
result = s1.intersection(s2, s3)
print(result)

Q24.
l1 = [101, 120, 88, 16, 14]
l2 = [88, 108, 66, 101, 140]
r = len(set(l1).intersection(l2))
#r = len(set(l1) & set(l2))
print(r)

Q25.
#Python intersection of multiple sets
s1 = {15, 18, 16, 20, 25}
s2 = {20, 14, 15, 12, 22}
s3 = {15, 12, 20, 23, 19}
i = set.intersection(s1,s2,s3)
print(i)

Q26.
set1 = {10, 12, 8, 6, 4}
set2 = {8, 18, 6, 10, 5}
r = len(set(set1) & set(set2))
print(r)

#
set1 = {10, 12, 8, 6, 4}
set2 = {8, 18, 6, 10, 5}
r = len((set1) & (set2))
print(r)

#
set1 = {10, 12, 8, 6, 4}
set2 = {8, 18, 6, 10, 5}
r = len(set1.intersection(set2))
print(r)

Q27.
set1 = {1,2,3,4,5}
set2 = {2,3,5,7,9}
set3 = {3,5,10,11,12}
set4 = {8,7,5,4,3}
result = set1 & set2 & set3 & set4
print(result)

Q28.
S1 = {1,2,3,4,5,6}
S2 = {5,6,7,8,9}
print(S1.difference(S2))
print(S2.difference(S1))

#
S1 = {1,2,3,4,5,6}
S2 = {5,6,7,8,9}
S = S1 - S2
print(S)

S1 = {1,2,3,4,5,6}
S2 = {5,6,7,8,9}
S = S2 - S1
print(S)

S1 = {1,2,3,4,5,6}
S2 = {5,6,7,8,9}
S = S2 - S2
print(S)

Q29.
S1 = {1,2,3,4,5,6}
S2 = {5,6,7,8,9}
print(S1.symmetric_difference(S2))
print(S2.symmetric_difference(S1))

Q30.
s = {1,2,3}
print(1 in s)

s = {1,2,3}
#print(1 in s)

for i in s:
print(i, end = "")

s = {1,2,3}
for i, v in enumerate(s):
print(i,v)

Q31.

x = {p for p in range(10)}
print(x)
Q32.
y = {q*q for q in range(12, 15) if q%2==0}
print(y)

Answer: {144, 196}

Q.33
y = {q*q for q in range(12, 15) if q%2!=0}
print(y)

Answer: {169}

You might also like