tuple
ํํ(tuple)์ ๋ฆฌ์คํธ(list) ๋น๊ต
: ๋ช ๊ฐ์ง ์ ์ ์ ์ธํ๋ฉด ๋ฆฌ์คํธ์ ๊ฑฐ์ ๋น์ทํ๋ค
๋ฆฌ์คํธ(list)๋ [ ]์ผ๋ก ๋๋ฌ์ธ์ง๋ง ํํ(tuple)์ ( )์ผ๋ก ๋๋ฌ์ผ๋ค.
๋ฆฌ์คํธ(list)๋ ๊ทธ ๊ฐ์ ์์ฑ, ์ญ์ , ์์ ์ด ๊ฐ๋ฅํ์ง๋ง ํํ(tuple)์ ๊ทธ ๊ฐ์ ๋ฐ๊ฟ ์ ์๋ค.
์ด์ฒ๋ผ ํ๋ก๊ทธ๋จ์ด ์คํ๋๋ ๋์ ๊ทธ ๊ฐ์ด ํญ์ ๋ณํ์ง ์๊ธฐ๋ฅผ ๋ฐ๋๋ค๋ฉด ํํ(tuple)์ ์ฌ์ฉํ๋ค
ํํ
t1 = () # ๊ธฐ๋ณธ
t2 = (1,) # ๋ฆฌ์คํธ์ ๋ค๋ฅด๊ฒ 1๊ฐ์ ์์๋ง ๊ฐ์ง๋๋ ์ฝค๋ง(,)๋ฅผ ๋ฐ๋์ ๋ถ์ฌ์ผ ํ๋ค
t3 = (1, 2, 3)
t4 = 1, 2, 3 # ๊ดํธ()๋ฅผ ์๋ตํด๋ ๋ฌด๋ฐฉํ๋ค
t5 = ('a', 'b', ('ab', 'cd'))
ํํ์ ์ญ์ ์ ๋ณ๊ฒฝ : error ๋ฐ์
ํํ(tuple)์ ์์๊ฐ์ ์ญ์ ๋ฐ ๋ณ๊ฒฝํ๋ฉด error๊ฐ ๋ฐ์ํ๋ค
```python
t1 = (1, 2, 'a', 'b')
del t1[0]
Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object doesn't support item deletion
```python
t1 = (1, 2, 'a', 'b')
t1[0] = 'c'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
ํํ ๋ค๋ฃจ๊ธฐ
์ธ๋ฑ์ฑ
t1 = (1, 2, 'a', 'b') print(t1[0]) # 1
์ฌ๋ผ์ด์ฑ
t1 = (1, 2, 'a', 'b') print(t1[1:]) # (2, 'a', 'b')
๋ํ๊ธฐ
t1 = (1, 2, 'a', 'b') t2 = (3, 4) print(t1 + t2) # (1, 2, 'a', 'b', 3, 4)
๊ณฑํ๊ธฐ
t2 = (3, 4) print(t2 * 3) # (3, 4, 3, 4, 3, 4)
๊ธธ์ด ๊ตฌํ๊ธฐ
t1 = (1, 2, 'a', 'b') print(len(t1)) # 4
Last updated
Was this helpful?