๐Ÿงบ Python Set โ€” Unique, Unordered, Powerful Collection

Introduction ๐ŸŒŸ

A set in Python is an unordered collection of **unique** elements. Sets automatically remove duplicates and are optimized for fast membership checking. They are ideal for tasks involving uniqueness, mathematical operations, and filtering.

Note

๐Ÿ’ก Sets are defined using curly braces or the set() constructor.

1. Creating Sets ๐Ÿงฑ

create_set.py

# Empty set (IMPORTANT: {} is a dict)
empty_set = set()

# Set with values
numbers = {10, 20, 30}

# Set automatically removes duplicates
unique_nums = {1, 2, 2, 3, 3, 4}  # {1, 2, 3, 4}

# Mixed data types
mixed = {10, "apple", 3.5, True}

Note

โœ”๏ธ Sets do NOT allow duplicates.
โœ”๏ธ Sets are unordered โ†’ No indexing.

2. Accessing Elements โŒโžก๏ธโœ”๏ธ

Sets do NOT support indexing like lists or tuples.

access_set.py

fruits = {"apple", "banana", "mango"}

for item in fruits:
    print(item)

Note

โœ”๏ธ You can loop through the set to access items.
โœ”๏ธ Order is NOT guaranteed.

3. Adding Elements โž•

add_set.py

fruits = {"apple", "banana"}
fruits.add("mango")
print(fruits)

4. Adding Multiple Items โž•โž•

update_set.py

fruits.update(["grape", "kiwi"])
print(fruits)

5. Removing Elements โž–

remove() โ€” Errors if item not found

remove_set.py

fruits.remove("banana")

discard() โ€” Safe removal

discard_set.py

fruits.discard("orange")  # No error even if 'orange' not found

pop() โ€” Removes a random element

pop_set.py

fruits.pop()

clear() โ€” Remove all elements

clear_set.py

fruits.clear()

6. Set Operations (Most Powerful Feature) โšก

Union ( | ) โ€” Combine Sets

union_set.py

a = {1, 2, 3}
b = {3, 4, 5}

print(a | b)   # {1, 2, 3, 4, 5}

Intersection ( & ) โ€” Common Elements

intersection_set.py

print(a & b)   # {3}

Difference ( - ) โ€” Items in A not in B

difference_set.py

print(a - b)   # {1, 2}

Symmetric Difference ( ^ ) โ€” Items NOT in both

symmetric_difference.py

print(a ^ b)   # {1, 2, 4, 5}

Note

๐Ÿ’ก Set operations are extremely fast due to hashing.

7. Checking Membership ๐Ÿ”

membership_set.py

nums = {10, 20, 30}

print(20 in nums)        # True
print(25 not in nums)    # True

8. Frozen Set โ€” Immutable Set ๐ŸงŠ

A frozenset is an immutable version of a set. It cannot be changed after creation.

frozenset.py

fs = frozenset([1, 2, 3])
print(fs)

9. Set Comprehension โšก

set_comprehension.py

squares = {x*x for x in range(1, 6)}
print(squares)

Note

โœ”๏ธ Similar to list comprehension but produces a set.

10. Real-World Examples ๐ŸŒ

Removing Duplicates From a List

remove_duplicates.py

nums = [1, 2, 2, 3, 3, 4]
unique = list(set(nums))
print(unique)

Checking Common Interests

common_interests.py

person1 = {"music", "sports", "coding"}
person2 = {"coding", "movies"}

print(person1 & person2)  # intersection

Filtering Unique Words

filter_words.py

sentence = "apple banana apple mango banana"
words = set(sentence.split())
print(words)

Conclusion ๐ŸŽ‰

>>โ€œSets bring speed, uniqueness, and mathematical power to Python โ€” simple yet incredibly useful.โ€ โœจ

You now fully understand Python Sets! Want the next topic? Try Dictionary, List Comprehension, Functions, or File Handling. Just tell me! ๐Ÿ˜Š