Python Set Methods: Union, Intersection, and More

📌 Topic: Set Methods in Python Today I explored Set Methods in Python. A set is an unordered collection of unique elements. It does not allow duplicates and is very useful for mathematical operations like union and intersection. 🔹 Important Set Methods I Learned:  add() – Adds a single element to the set s = {1, 2, 3} s.add(4) print(s) ✅ update() – Adds multiple elements s.update([5, 6]) ✅ remove() – Removes an element (gives error if not found) ✅ discard() – Removes an element (no error if not found) ✅ pop() – Removes a random element ✅ clear() – Removes all elements 🔹 Set Operations: 🔸 Union (| or union()) 🔸 Intersection (& or intersection()) 🔸 Difference (- or difference()) 🔸 Symmetric Difference (^) Example: a = {1, 2, 3} b = {3, 4, 5} print(a.union(b)) print(a.intersection(b)) 📚 Every day I am improving step by step in Python. Consistency is the key to success! 💪 #Day16 #PythonLearning #SetMethods #CodingJourney #LearningEveryday

To view or add a comment, sign in

Explore content categories