선언 방법 , 팩킹

t = ('so', 'ye', 'on', '2')
t2 = 's',

('so', 'ye', 'on', '2') <class 'tuple'>
('s',) <class 'tuple'>

 

튜플 선언에서 괄호 () 는 생략 가능 콤마(,) 만 있으면 됨!

원소가 하나일 때는 원소 마지막에 콤마(,) 를 붙이면 tuple

 

언팩킹

(x1, x2, x3, x4) = t

print(type(x1), type(x2), type(x3), type(x4))
print(x1, x2, x3, x4)

<class 'str'> <class 'str'> <class 'str'> <class 'str'>
so ye on 2

 
 
묶여있던걸 (팩킹) 풀어서 (언팩킹) 각각 변수에 대입
괄호 ()를 생략하고 
x1, x2, x3, x4 = t로 해도 되지만 관습상 괄호 ()를 사용

 

'Python > 파이썬 초급' 카테고리의 다른 글

[Python] 가변인자  (0) 2022.11.14
[Python] for-else 문  (0) 2022.11.11
[Python] 딕셔러니 (dictionary)  (0) 2022.11.11
[Python] print (escape, rawstring, multiline)  (1) 2022.11.11
[Python]__name__  (0) 2022.10.19