TUPLES
- 리스트랑 똑같은데 데이터 추가 삭제 변경이 안된다. (Immutable Python objects.)
- 튜플은 시퀀스, 즉 순서가 있다.
- 튜플은 괄호 '( )' 를 사용한다.
정수, 문자열 실수로 튜플을 만들자
in: my_tuple = (1,'Hello',6.555)
in: my_tuple[1]
out:
'Hello'
튜플에서 편집을 시도해보자
튜플에 데이터 추가
in: my_tuple.append(100)
out:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8332/3849163340.py in <module>
----> 1 my_tuple.append(100)
AttributeError: 'tuple' object has no attribute 'append'
튜플에서 데이터 변경
in: my_tuple[0]=100
out:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8332/285723970.py in <module>
----> 1 my_tuple[0]=100
TypeError: 'tuple' object does not support item assignment
튜플에서 데이터 제거
in: del my_tuple [0]
out:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_8332/3138307070.py in <module>
----> 1 del my_tuple [0]
TypeError: 'tuple' object doesn't support item deletion
※ 튜플에서 모든 편집이 불가능하다. 굳이 변경하려면 튜플을 다시 설정해서 데이터를 덮어씌우는 방법밖에 없다.
# 변경이 불가능한 튜플의 특성상 보안이 필요한 상황에 쓰인다.
SETS
- 세트에 들어있는 데이터는 순서가 없다.
- 세트에는 동일한 값이 저장되지 않는다. (집합과 같다.)
- 세트는 중괄호 '{ }' 로 정의한다.
중복되는 수를 포함시켜 세트를 만들자
in: my_set = {1,2,3,1,1,1,1,1,1}
in: my_set
out:
{1, 2, 3}
# 세트는 중복되는 수를 하나로 만드는 특징이 있다.
# 사용 예시로, len함수와 함께 사용하여 페이지 방문한 특정 유저수를 조사할때 쓰인다.
add 함수를 사용하여 세트에 데이터를 추가할때
in: my_set.add(100)
in: my_set
out:
{1, 2, 3, 100}
discard 함수를 사용하여 세트의 데이터를 삭제할수 있다.
in: my_set.discard(3)
in: my_set
out:
{1, 2, 100}
두개의 세트의 관계를 설명할수 있다.
두 세트의 합집합을 구할때
in: event_A = {5,10,11,111}
in: event_B = {1,5,10,200}
in: event_A | event_B
out:
{1, 5, 10, 11, 111, 200}
# ' |' 를 통해 세트의 합집합을 구할수 있다. (Shift + \ )
두 세트의 교집합을 구할때
in: event_A & event_B
out:
{5, 10}
# '&' 를 통해 세트의 교집합을 구할수 있다.
두 세트의 차집합을 구할때
in: event_B - event_A
out:
{1, 200}
# ' - ' 를 통해 세트의 차집합을 구할수 있다.
'Python' 카테고리의 다른 글
[파이썬] 반복문: for, in, while, break, enumerate, range (0) | 2022.04.25 |
---|---|
[파이썬] 비교문, 논리연산자, 조건문 : ==, !=, >, <, and, or, if, elif, else (0) | 2022.04.21 |
[파이썬] 딕셔너리와 불린: get, keys, values, items, clear, zip, update, True, False (1) | 2022.04.20 |
[파이썬] 리스트: append, insert, remove, pop, index, in, sorted (0) | 2022.04.19 |
[파이썬] 문자열: upper, lower, slicing, split, replace, find, len, strip (0) | 2022.04.19 |
댓글