튜플(Tuple) : 프로그램 실행 중 값을 변경하지 않고 쓸 때 사용하는 자료형이다.

선언

# 선언 - 리스트와 선언 방식 차이가 있음
t1 = (1, 2) # 소괄호 사용
print(t1) # (1, 2)
print(type(t1)) # tuple

t2 = (1)
print(t2) # 1
print(type(t2)) # int

# 튜플에 한개의 요소만 있을때는 뒤에 ,(쉼표)를 붙여야 한다.
t3 = (1, )
print(t3) # (1,)
print(type(t3)) # tuple

# 소괄호를 생략할 수 있다.
t4 = 1, 2,3
print(t4) # (1, 2, 3) - 알아서 공백 넣어줌
print(type(t4)) # tuple

# 중첩 Tuple
t5=('a', 'b', 'c', ('ab', 'cd'))
print(t5)

# 튜플의 리스트와 다른점 : 요소의 변경을 할수가 없다.
t6 = (1,2,'a','b')
print(t6) # (1, 2, 'a', 'b')
del t6[0] # TypeError: 'tuple' object doesn't support item deletion

t7 = (1,2,'a','b')
t7[0] = 3 # TypeError: 'tuple' object doesn't support item deletion

슬라이싱

t8 = (1, 2, 'a', 'b')
print(t8[2:]) # ('a', 'b')

연산

# 연산
print(t7 + t8) # (1, 2, 'a', 'b', 1, 2, 'a', 'b')