list형과 비슷하지만 한 번 생성되면 값을 변경할 수 없습니다.
t = ("감자", 1, 3.14, 0)
print(t)
type(t)
-----------------------------------
#result
('감자', 1, 3.14, 0)
<class 'tuple'>
t = ("감자", 1, 3.14, 0)
print(t[0])
print(t[1])
-----------------------------------
#result
감자
1
t = ("감자", 1, 3.14, 0)
print(len(t))
-----------------------------------
#result
4
t = ("감자", 1, 3.14, 0)
for i in t:
print(i)
-----------------------------------
#result
감자
1
3.14
0
t = ("감자", 1, 3.14, 0)
t = t + ('사랑' ,'해')
print(t)
-----------------------------------
#result
('감자', 1, 3.14, 0, '사랑', '해')
t = ("감자", 1, 3.14, 0)
print(t*2)
-----------------------------------
#result
('감자', 1, 3.14, 0, '감자', 1, 3.14, 0)
a = (('나무' ,2) , ('새',4), (6,'감기'))
print(a[2])
print(a[2][1])
-----------------------------------
#result
(6, '감기')
감기
but 회피하는 방법이 있습니다. 한개의 원소 뒤에 콤마를 찍어주면 tuple이 유지됩니다.
a = (5000)
b = (5000,)
print(type(a))
print(type(b))
# print(len(a)) # TypeError: object of type 'int' has no len()
print(len(b))
-----------------------------------
#result
<class 'int'>
<class 'tuple'>
1
a = 1, 3, 2, 5, 7
print(type(a))
-----------------------------------
#result
<class 'tuple'>
def minmax(items):
return min(items), max(items)
print(minmax([-5, 6, 7, 8, 55, 96, 785, 362]))
-----------------------------------
#result
(-5, 785)
def minmax(items):
return min(items), max(items)
lower, upper = minmax([-5, 6, 7, 8, 55, 96, 785, 362])
print(lower)
print(upper)
print(lower, upper)
-----------------------------------
#result
-5
785
-5 785
(a, (b,(c, d))) = (19,(5,(3,1)))
print(a)
print(b)
print(c)
print(d)
-----------------------------------
#result
19
5
3
1
a = '딸기'
b = '토마토'
a, b = b, a
print(a, b)
-----------------------------------
#result
토마토 딸기
print(tuple([1, 7, 5, 3, 9]))
print(tuple("abcde"))
print(tuple({'abc': 5, 'def': 2, 'ghi': 999}))
# print(tuple(12345)) # TypeError: 'int' object is not iterable
-----------------------------------
#result
(1, 7, 5, 3, 9)
('a', 'b', 'c', 'd', 'e')
('abc', 'def', 'ghi')
a = 'a' in ('a', 'b', 'c')
b = 5 in ('a','b','c')
c = 5 not in ('a','b','c')
print(a, b, c)
-----------------------------------
#result
True False True