028-001. creating set, differnce between list/dictionary and set, frozenset
@ # Python provides data type which expreses "set" @ # I make fruits set which contain 5 elements fruits = {'strawberry', 'grape', 'orange', 'pineapple', 'cherry'} print(fruits) # {'pineapple', 'orange', 'grape', 'strawberry', 'cherry'} @ # Elements which are involved in set are unordered # So, whenever you print set, the order of elements is different @ # You can't put duplicated element into set # Actually, if you put duplicated element into set, the only one element is put fruits = {'orange', 'orange', 'cherry'} print(fruits) # {'cherry', 'orange'} @ # Unlike list and tuple, you can partial element from set by using indexing sytax fruits = {'strawberry', 'grape', 'orange', 'pineapple', 'cherry'} print(fruits[0]) # Traceback (most recent call last): # File "<pyshell#42>", line 1, in <module> # print(fruits[0]) # TypeError: 'set' object does not support indexing fruits['strawberry'] # Traceback (most recent call last): # File "<pyshell#43>", line 1, in <module> # fruits['strawberry'] # TypeError: 'set' object is not subscriptable @ # This time, we will try to make set by using set(iterable_object) @ # If you pass string into set(), unique characters are put into set a = set('apple') print(a) # {'e', 'l', 'a', 'p'} @ b = set(range(5)) print(b) # {0, 1, 2, 3, 4} @ # You can make empty set by using set() c = set() print(c) # set() @ # The difference between dictionary type and set type c = {} type(c) # <class 'dict'> c = set() type(c) # <class 'set'> @ # I make set with string in Korean set('안녕하세요') # {'녕', '요', '안', '세', '하'} @ # You can't make set inside of set unlike dictionary and list a = {{1, 2}, {3, 4}} # Traceback (most recent call last): # File "<pyshell#3>", line 1, in <module> # a = {{1, 2}, {3, 4}} # TypeError: unhashable type: 'set' @ # Tip # Python privides "frozen set" whose set can't be changed frozen_set = frozenset(iterable_object) a = frozenset(range(10)) print(a) # frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) # You can't apply set operators(adding element, removing element) and method on frozenset a = frozenset(range(10)) a |= 10 # Traceback (most recent call last): # File "<pyshell#4>", line 1, in <module> # a |= 10 # TypeError: unsupported operand type(s) for |=: 'frozenset' and 'int' a.update({10}) # Traceback (most recent call last): # File "<pyshell#5>", line 1, in <module> # a.update({10}) # AttributeError: 'frozenset' object has no attribute 'update'