Python Set Methods

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

#Methods of Set Collection
# myset={10,"Indore",89.99,True}
# myset.add("Mumbai")

# print(myset)
# myset.clear()
# print(myset)
# myset1=myset
# print(myset)
# print(myset1)

# myset.add("Mumbai")
# myset.add("Mumbai")
# print(len(myset))
# print("Before Remove: ",myset)
# myset.remove("Indore")
# print("Before Remove: ",myset)
# print("Before Pop:",myset)
# myset.pop()
# print("After Pop:",myset)

# Return a set that contains all data elements from both sets, duplicates are excluded:

# myset1 = {"first", "second", "third"}
# myset2 = {"four", "five", "first"}
# myset3 = myset1.union(myset2) 
# print(myset3)

# Return a set that contains the items that only exist in set myset1, and not in set myset2:
# myset1 = {"first", "second", "third"}
# myset2 = {"four", "five", "first","third"}
# myset3 = myset1.difference(myset2) 
# print(myset3)

#Return a set that contains the items that exist in both set myset1, and set myset2:
# myset1 = {"first", "second", "third","four"}
# myset2 = {"four", "five", "first"}
# myset3 = myset1.intersection(myset2) 
# print(myset3)

# myset1 = {"first", "second", "third","four","five"}

# print("Before discard: ",myset1)

# myset1.discard("second")

# print("After discard: ",myset1)


# myset1 = {"first", "second", "third"}
# myset2 = {"four", "five", "first"}
# myset1.update(myset2)
# print(myset1)

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *