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?