Python Sets: Unordered Collections and Operations

𝗗𝗮𝘆 𝟭𝟰: 𝗦𝗲𝘁𝘀 𝗶𝗻 𝗣𝘆𝘁𝗵𝗼𝗻 🐍 𝗦𝗲𝘁𝘀: 🔹 Sets are unordered collections of items 🔹 Elements are unique (no duplicate values) 🔹 Items are separated by commas and enclosed in curly brackets { } 🔹 Sets do not maintain order 𝗖𝗿𝗲𝗮𝘁𝗶𝗻𝗴 𝗮 𝗦𝗲𝘁: 🔹 s = {1, 2, 3} 🔹 Use set() to create an empty set : s = set() ⚠️ {} creates an empty dictionary, not a set 𝗦𝗲𝘁 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀:  1️⃣𝗨𝗻𝗶𝗼𝗻 (|) → combines elements  2️⃣𝗜𝗻𝘁𝗲𝗿𝘀𝗲𝗰𝘁𝗶𝗼𝗻 (&) → common elements  3️⃣𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 (-) → elements in one set but not the other  4️⃣𝗦𝘆𝗺𝗺𝗲𝘁𝗿𝗶𝗰 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 (^) → elements not common 📝Important Notes: 🔹 Sets do not support indexing or slicing 🔹 Elements must be immutable (int, string, tuple) Building strong Python fundamentals, one concept at a time 🚀 #Python #Tuples #LearningPython #LearningInPublic #AspiringDataScientist #Consistency

To view or add a comment, sign in

Explore content categories