Sets & Dictionaries

There are another two types of data collection in Python:
1) Set, which is unordered and unindexed
and
2) Dictionary, which is also unordered, changeable but indexed. Both with no duplicate members.

SETS

#Sets

St1 = {6,10,5,1,9,4}
St2 = {"Ford", "Toyota", "Opel", "Suzuki"}
St3 = set((1,3,3,5,5,5,9,9,9,9))

print(St1)
print(St2)
print(len(St3)

L1 = list(S3)
Count_dup = {i:L1.count(i) for i in L1}
print(L1)

#Operations
St2.add("Audi") #cannot add more than one elem

#if we will try to use operator += same as for tuples and lists 
#we will received an error:
z = {"Violet", "Red",}
St2 +=z
print(St2)
Traceback (most recent call last):
  File "C:\Users\aszcz\Desktop\Python_cw\Sets_Dict.py", line 19, in <module>
    St2 +=z
TypeError: unsupported operand type(s) for +=: 'set' and 'set'
#but we have an update option:
St2.update(["BMW", "Fiat"])
print(St2)
#output: {"Ford", "Toyota", "Opel", "Suzuki", "BMW", "FIAT'}
#we need to add the elements inside the bracket [], 

#without it we will receive result an error for numbers:
''''Traceback (most recent call last):
    File "C:\Users\Agnieszka\PycharmProjects\file.py", 
    line 26, in <module>
    St2.update(10,11)
TypeError: 'int' object is not iterable'''

#and for strings  separeted letters as follow:
St2.update("BMW", "Fiat")
print(St2) 
#{'B', 'a', 'M', 'Opel', 'W', 'i', 'Suzuki', '..., 't', 'F'}
#removing
print(St2)
St2.remove("Audi")
print(St2)
St2.discard("BMW")
print(St2)
#Difference method
Exclude_Num = {2,4,6,8}
New_Numbers = Numbers - Exclude_Num
print(New_Numbers)

#Diffference_update function
Numbers.difference_update(Exclude_Num)
print(Numbers)

#Comprehension
Numbers = {elem for elem in Numbers if elem not in Exclude_Num}
print(Numbers)

#Loop
for elem in Exclude_Num:
    Numbers.discard(elem)

print(Numbers)


Now I will try to add one set to another in the same way I used to do it with lists and tuples:

St3 = St1+St2
print(St3)
We got error. Need to use some function.
St3 = St1.union(St2)
print(St3)
#adding list elems to set
St3 = {1,3,5,7,9}
Lst = [2,4,6,8]
St3.update(Lst)
print(St3)

#or
St4 = St3.union(Lst)
print(St4)

DICTIONARIES

Dct1 = {}
Dct2 = dict(one=1,two=2,three=3)
Dct3 ={"one":1, "two":2, "three":3}

print(Dct1)
print(Dct2)
print(Dct3)
#operations
print(Dct3["one"])
print("two" in Dct2)
print("five" in Dct3)
print(Dct3.keys())
print(Dct3.values())
print("jeden" in Dct3.keys())
#Loops
for x in Dct3.values():
    print(x)

for y in Dct3.keys():
    print(y)
Cos = {
    "brand": "Maybelline",
    "toxic": False,
    "cruelty-free": True,
    "exp_date": 2024,
    "model": 123458900,
    "name": 'mascara',
    "colors": ["black", "green", "blue"]
}

print(Cos)
print(type(Cos))
print(Cos['brand'])
print(Cos.values())
print(Cos.keys())
print(len(Cos))

El = Cos.get('model')
print(El)
Itm = Cos.items()
print(Itm)
print(Cos.items()[1:4])


#if we update using one value, previous element will be removed:
Cos.update({"colors":"yellow"})
print(Cos)
Cos.update({ "colors": ["black", "green", "blue", "yellow", "red"]})
print(Cos)
#removing
#The popitem() method removes the last inserted item:
Cos.popitem()
print(Cos)
del Cos['toxic']
del Cos['colors']
print(Cos)

Cos.clear()
print(Cos)
#Loops
for x in Cos:
  print(x)

for x in Cos:
    print(Cos[x])  #values as a result

#to get keys only:
for x in Cos.keys():
    print(x)

#to get all data from out Dictionary:
for x, y in Cos.items():
    print(x, y)
    
#Copy
CopyDict = dict(Cos)
print(CopyDict)

#Dict inception
mycosm = {
  "cosm1" : {
    "name" : "Mascara",
    "exp_date" : 2023
  },
  "cosm2" : {
    "name" : "Lipstick",
    "exp_date" : 2025
  },
  "cosm3" : {
    "name" : "Face Powder",
    "exp_date" : 2024
  },
}

#or
cosm1 = {
    "name" : "Mascara",
    "exp_date" : 2023
}
cosm2 = {
    "name" : "Lipstick",
    "exp_date" : 2025
}
cosm3 = {
    "name" : "Face Powder",
    "exp_date" : 2024
}

mycosm = {
    "cosm1" : cosm1,
    "cosm2" : cosm2,
    "cosm3" : cosm3
}


print(mycosm)