Sets are collections of unique, unordered elements. They are like lists but do not allow duplicate values.

Properties of sets:

  1. Unique elements: No duplicates allowed.
  2. Unordered: No specific order.
  3. Mutable: You can add or remove items.
  4. Not indexed: Sets do not support indexing or slicing.

download (5).png

Creating a Set:

my_set = {1, 2, 3, "Tarif"}
print(my_set)  # Output: {1, 2, 3, 'Tarif'}

If you try to add a duplicate value to a set, it will be ignored.

my_set = {1, 2, 3, 3, 3, 3, "Tarif",4, 4, 5, "Tarif"}
print(my_set)  
**# Output: {1, 2, 3, 4, 'Tarif', 5}**

print(len(my_set))
**# Output: 6**

Creating Empty Set:

# Creating an empty set (use set() not {})
empty_set = set()
print(empty_set)  # Output: set()