python program Implement use of Sets and various operations on Sets. like insert, update, and delete

in this python programming tutorial we learn about implementation uses of set and various operation on set like insert, update, and delete


the following syntax is use to insert the element in to the set

print("initial empty set:",set1)

set1.add("red")


the following syntax is use to update the element in the set

print("After entering 1 element:",set1)

set1.update(["yellow","blue"])

print("After updating more element :",set1)


the following syntax is use to delete the element in the set


if "red" in set1:

    set1.remove("red")

print("After removing  (red) element:",set1)


code

set1=set()
print("initial empty set:",set1)
set1.add("red")
print("After entering 1 element:",set1)
set1.update(["yellow","blue"])
print("After updating more element :",set1)
if "red" in set1:
    set1.remove("red")
print("After removing  (red) element:",set1)
print(set1)
for item in set1:
    print(item)
print("Item count:",len(set1))
isempty=len(set1)==0
set1=set(["red","yellow"])
set2=set(["yellow","blue"])
set3=set1 & set2
set4=set1 | set2
set5=set1 - set2
set6=set1 ^ set2
issubset=set1 <= set2
issuperset=set1 >= set2
set7= set1.copy()
set7.remove("red")
set8=set1.copy()
set8.clear()
print("original set:",set1)
print("original set:",set2)
print("intersection  set1 & set2:",set3)
print("union set1 | set2:",set4)
print("set diffrence(set1 - set3):",set5)
print("symmetric diffrence(set1 ^ set3):",set6)
print("subset test(set1 <= set2):",issubset)
print("superset test(set1 >= set2):",issuperset)
print("shallow copy:",set7)
print("after clearing:",set8)



output:

Implement use of Sets and various operations on Sets. like insert, update, and delete



Post a Comment

0 Comments