set - ibrahimrifats/Back-End-development GitHub Wiki

set doesn't allow duplicate values and this always prints data sorted

**1. Sets do not allow duplicate values. 2. Sets are unordered collections, but they do use a hashing mechanism that causes elements to be printed in a sorted order.

Let me correct the examples and include this information:

Immutable Data Types:

  1. Numbers: int, float, complex
  2. Strings
  3. Tuples
  4. Frozen Sets

Mutable Data Types:

  1. Lists
  2. Sets
  3. Dictionaries ** Here are the examples with the corrected information:

Immutable:

# Numbers
my_integer = 42
my_float = 3.14
my_complex = 1 + 2j

# Strings
my_string = "Hello, World!"

# Tuples
my_tuple = (1, 2, 3, "apple")

# Frozen Sets
my_frozen_set = frozenset([1, 2, 3])

Mutable:

# Lists
my_list = [1, 3, 4, 5, True, False, 'rifat']

# Sets
my_set = {1, 2, 3, 'apple'}

# Dictionaries
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

For sets, the elements are indeed printed in a sorted order due to their internal hashing mechanism. However, it's important to note that this sorted order is not based on the order of insertion, and the order may change when elements are added or removed from the set. Sets are primarily used for membership tests and to eliminate duplicate elements from a collection.

set_a = {1,2,3,4,5,5}
print(set_a)

set_a.add(6)
set_a.add(5)

print(set_a)

set_b = {1,2,3,3,5,12,14,15}
#union operation 
print(set_a.union(set_b))
#for the other option for union operation we can use the "|" vertical symbol
print(set_a | set_b)

#intersection operation
print(set_a.intersection(set_b))

# For intersection operation we can use &
print(set_a & set_b)

print(set_a.difference(set_b))

# We can use the difference by the '-' symbol
print(set_a - set_b)

#symetric difference 
print(set_a.symmetric_difference(set_b))

#symetric difference using ^ operator
symmetric_diff = set_a ^ set_b
print(symmetric_diff)

# set doesn't contain an ordered index of all elements inside
#print(set_a[0])