01_Python
24_컬렉션>튜플
chuuvelop
2025. 1. 7. 01:20
728x90
01. 튜플
- 리스트와 유사하지만 튜플은 값의 추가, 삭제, 수정이 불가
- 프로그램이 실행되는 동안 값이 변하면 안되는 경우 주로 사용
tu = (2, 2, 3 ,4, 5) #튜플: 요소의 추가 수정 삭제가 안됨
del tu[0]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[28], line 1
----> 1 del tu[0]
TypeError: 'tuple' object doesn't support item deletion
tu[0] = 0
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[29], line 1
----> 1 tu[0] = 0
TypeError: 'tuple' object does not support item assignment
02. 인덱싱, 슬라이싱
tu[0]
2
tu[1:]
(2, 3, 4, 5)
03. 튜플 연산
tu1 = (1, 2)
tu2 = (3, 4)
print(tu1 + tu2)
(1, 2, 3, 4)
tu1
(1, 2)
tu2
(3, 4)
# tu1, tu2에는 아무런 영향을 미치지 않았음(비파괴적)
print(tu1 *2)
(1, 2, 1, 2)
728x90