: 객체에 속한 함수, 객체의 상태를 조작하거나 동작을 수행
데이터 타입 객체.메서드()
ex) 'hello'.capitalize()
: 고유한 항목들(중복 x)의 정렬되지 않은(비시퀀스) 컬렉션
my_set = {'a', 'b', 'c', 1, 2, 3}
my_set.add(4)
print(my_set) #{'a', 1, 2, 3, 4, 'b', 'c'}
my_set.add(4)
print(my_set) #{'a', 1, 2, 3, 4, 'b', 'c'}
my_set = {'a', 'b', 'c', 1, 2, 3}
my_set.clear()
print(my_set) #set()
my_set = {'a', 'b', 'c', 1, 2, 3}
my_set.remove(2)
print(my_set) #{1, 3, 'a', 'c', 'b'}
my_set.remove(10)
print(my_set) #KeyError: 10
my_set = {'a', 'b', 'c', 1, 2, 3}
element = my_set.pop()
print(element) #1
print(my_set) #{2, 3, 'b', 'c', 'a'}
my_set = {1, 2, 3}
my_set.discard(2)
print(my_set) #{1, 3}
my_set.discard(10)
print(my_set) #{1, 3}
my_set = {'a', 'b', 'c', 1, 2, 3}
my_set.update([1, 4, 5])
print(my_set) #{1, 2, 'c', 3, 'b', 4, 5, 'a'}
set1 = {0, 1, 2, 3, 4}
set2 = {1, 3, 5, 7, 9}
print(set1.difference(set2)) #{0, 2, 4}
print(set1.intersection(set2)) #{1, 3}
print(set1.issubset(set2)) #False
print(set1.issuperset(set2)) #False
print(set1.union(set2)) #{0, 1, 2, 3, 4, 5, 7, 9}
: 고유한 항목들의 정렬되지 않은 컬렉션
person = {'name': 'Alice', 'age': 25}
person.clear()
print(person) #{}
person = {'name': 'Alice', 'age': 25}
print(person.get('name')) #Alice
print(person.get('country')) #None
print(person.get('country', 'Unknown')) #Unknown
person = {'name': 'Alice', 'age': 25}
print(person.keys()) #dict_keys(['name', 'age'])
for k in person.keys():
print(k)
"""
name
age
"""
person = {'name': 'Alice', 'age': 25}
print(person.keys()) #dict_keys(['name', 'age'])
for v in person.values():
print(v)
"""
Alice
25
"""
person = {'name': 'Alice', 'age': 25}
print(person.items()) #dict_items([('name', 'Alice'), ('age', 25)])
for k, v in person.items():
print(k, v)
"""
name Alice
age 25
"""
person = {'name': 'Alice', 'age': 25}
print(person.pop('age')) #25
print(person) #{'name': 'Alice'}
print(person.pop('country', None)) #None
print(person.pop('country')) #keyError
person = {'name': 'Alice', 'age': 25}
print(person.setdefault('country', 'KOREA')) #KOREA
print(person)
person = {'name': 'Alice', 'age': 25}
other_person = {'name': 'Jane', 'gender': 'Female'}
person.update(other_person)
print(person) #{'name': 'Jane', 'age': 25, 'gender': 'Female'}
person.update(age=50)
print(person) #{'name': 'Jane', 'age': 50, 'gender': 'Female'}
person.update(country='KOREA')
print(person) #{'name': 'Jane', 'age': 50, 'gender': 'Female', 'country': 'KOREA'}
: 해시 함수를 사용하여 변환한 값을 색인(index)으로 삼아 키(key)와 데이터(value)를 저장하는 자료구조
-> 데이터를 효율적으로 저장하고 검색하기 위해 사용

my_set = {3, 2, 1, 9, 100, 4, 87, 39, 10, 52}
print(my_set.pop()) #1
print(my_set.pop()) #2
print(my_set.pop()) #3
print(my_set.pop()) #100
print(my_set.pop()) #4
print(my_set.pop()) #39
print(my_set.pop()) #9
print(my_set.pop()) #10
print(my_set.pop()) #52
print(my_set.pop()) #87
print(my_set) #set()
my_str_set = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
print(my_str_set.pop())
print(my_str_set.pop())
print(my_str_set.pop())
print(my_str_set.pop())
print(my_str_set.pop())
print(hash(1)) #1
print(hash(1)) #1
*같은 정수는 항상 같은 해시 값을 가짐
*해시 테이블에 정수를 저장할 때 효율적인 방법
*예를 들어, hash(1)과 hash(2)는 항상 서로 다른 해시 값을 갖지만, has(1)은 항상 동일한 해시 값을 갖게 됨
print(hash('a')) #실행시마다 다름
print(hash('a')) #실행시마다 다름
*문자열은 가변적인 길이를 갖고 있고, 문자열에 포함된 각 문자들의 유니코드 코드 포인트 등을 기반으로 해시 값을 계산
*이로 인해 문자열의 해시 값은 실행 시마다 다르게 계산됨
print(hash(1))
print(hash(1.0))
print(hash('1'))
print(hash((1, 2, 3)))
#TypeError: unhashable type: 'list'
print(hash((1, 2, [3, 4])))
#TypeError: unhashable type: 'list'
print(hash([1, 2, 3]))
#TypeError: unhashable type: 'list'
my_set = {[1, 2, 3], 1, 2, 3, 4, 5}
#TypeError: unhashable type: 'set'
my_dict = {{3, 2}: 'a'}