# tuple

## 튜플(tuple)와 리스트(list) 비교

: 몇 가지 점을 제외하면 리스트와 거의 비슷하다

* 리스트(list)는 \[ ]으로 둘러싸지만 튜플(tuple)은 ( )으로 둘러싼다.
* 리스트(list)는 그 값의 생성, 삭제, 수정이 가능하지만 튜플(tuple)은 그 값을 바꿀 수 없다.
* 이처럼 프로그램이 실행되는 동안 그 값이 항상 변하지 않기를 바란다면 튜플(tuple)을 사용한다&#x20;

## 튜플

```python
t1 = () # 기본 
t2 = (1,) # 리스트와 다르게 1개의 요소만 가질때는 콤마(,)를 반드시 붙여야 한다
t3 = (1, 2, 3)
t4 = 1, 2, 3 # 괄호()를 생략해도 무방하다 
t5 = ('a', 'b', ('ab', 'cd'))
```

## 튜플의 삭제와 변경 : error 발생

* 튜플(tuple)은 요솟값을 삭제 및 변경하면 error가 발생한다&#x20;

  \`\`\`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
````

## 튜플 다루기

* 인덱싱

  ```python
  t1 = (1, 2, 'a', 'b')
  print(t1[0]) # 1
  ```
* 슬라이싱

  ```python
  t1 = (1, 2, 'a', 'b')
  print(t1[1:]) # (2, 'a', 'b')
  ```
* 더하기

  ```python
  t1 = (1, 2, 'a', 'b')
  t2 = (3, 4)
  print(t1 + t2) # (1, 2, 'a', 'b', 3, 4)
  ```
* 곱하기

  ```python
  t2 = (3, 4)
  print(t2 * 3) # (3, 4, 3, 4, 3, 4)
  ```
* 길이 구하기

  ```python
  t1 = (1, 2, 'a', 'b')
  print(len(t1)) # 4
  ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://juneglee.gitbook.io/til/python/tuple.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
