728x90
  • 튜플 : 값을 변경하거나 삭제 시 오류 발생

>>> t1 = (1,2,'a','b')
>>> t1[0] = 'c'
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    t1[0] = 'c'
TypeError: 'tuple' object does not support item assignment
>>> del t1[0]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    del t1[0]
TypeError: 'tuple' object doesn't support item deletion '
# 인덱싱
>>> t1[0]
1
>>> t1[3]
'b'
# 슬라이싱
>>> t1[1:]
(2, 'a', 'b')
# 더하기
>>> t2 = (3,4)
>>> t1 + t2
(1, 2, 'a', 'b', 3, 4)
# 요소 한개만 더하기
>>> t1 = (1,2,'a','b') + (3,)
>>> t1
(1, 2, 'a', 'b', 3)
# 곱하기
>>> t2 * 3
(3, 4, 3, 4, 3, 4)
  • 딕셔너리 : Key를 통해 Value를 얻는다.

>>> dic = {'name':'park','phone':'0101111111','birth':'1224'}
# 쌍 추가하기
>>> a = {1:'a'}
>>> a[2] = 'b'
>>> a
{1: 'a', 2: 'b'}
# 요소 삭제하기
>>> del a[1]
>>> a
{2: 'b'}
  • Key, Value 사용하기

# Key 이용해 Value 얻기
>>> dic = {'name':'park','phone':'0101111111','birth':'1224'}
>>> dic['name']
'park'
>>> dic['name']
'park'
# Key를 같게하면 안됨
>>> a = {1:'a',1:'b'}
>>> a
{1: 'b'}
# Key 리스트 만들기
>>> dic.keys()
dict_keys(['name', 'phone', 'birth'])
# Value 리스트 만들기
>>> dic.values()
dict_values(['park', '0101111111', '1224'])
# Key, Value 쌍 얻기
>>> dic.items()
dict_items([('name', 'park'), ('phone', '0101111111'), ('birth', '1224')])
# 해당 키가 딕셔너리 안에 있는지 조사하기
>>> 'name' in dic
True
>>> 'address' in dic
False
# 딕셔너리 비우기
>>> dic.clear()
>>> dic
{}
  • 집합 : 집합과 관련된 것을 처리하기 위한 자료형. 중복 허용x, 순서가 없다.

>>> s1 = set([1,2,3])
>>> s1
{1, 2, 3}
>>> s2 = set("Hello")
>>> s2
{'H', 'o', 'l', 'e'}
# 교집합
>>> s1 = set([1,2,3,4,5])
>>> s2 = set([3,4,5,6,7])
>>> s1 & s2
{3, 4, 5}
>>> s1.intersection(s2)
{3, 4, 5}
# 합집합
>>> s1 | s2
{1, 2, 3, 4, 5, 6, 7}
>>> s1.union(s2)
{1, 2, 3, 4, 5, 6, 7}
# 차집합
>>> s1 - s2
{1, 2}
>>> s1.difference(s2)
{1, 2}
>>> s2 - s1
{6, 7}
>>> s2.difference(s1)
{6, 7}
# 값 1개 추가하기
>>> s1.add(4)
>>> s1
{1, 2, 3, 4}
# 값 여러개 추가하기
>>> s1.update([5,6,7])
>>> s1
{1, 2, 3, 4, 5, 6, 7}
# 특정 값 제거하기
>>> s1.remove(3)
>>> s1
{1, 2, 4, 5, 6, 7}
728x90

+ Recent posts