튜플

Kaydenna92·2023년 4월 17일
0

Python

목록 보기
7/10

특징

  • 항상 괄호로 둘러쌓여있다.
  • 불변
  • 튜플의 요소들은 언 패킹이나, 인덱싱으로 접근한다.
  • 리스트 같은 가변 객체들을 포함하는 튜플을 만들 수 있다.
# 튜플 선언
empty = ()
singleton = 'hello', # <-- note trailing comma
len(empty) # 0
len(sibgleton) # 1
singleton # ('hello',)
t = 12345, 54321, 'hello' # tuple packing
>>> t
(12345, 54321, 'hello')

>>> t[0]
12345

u = t, (1,2,3,4,5)
>>> u
((12345, 54321, 'hello'), (1, 2, 3, 4, 5))

t[0] = 99999
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

x, y, z = t # sequence unpacking # 어떤 시퀀스가 와도 됨.
>>> x, y, z 
>>> x
12345
>>> y
54321
>>> z
'hello'

# but they can contain mutable objects:
v = ([1,2,3], [4,5,6])
>>> v
([1, 2, 3], [4, 5, 6])
v[0][0] = 2
>>> v
([2, 2, 3], [4, 5, 6])
profile
persistently

0개의 댓글