---
# System prepended metadata

title: Python-元組tuple
tags: [python]

---

# Python-元組tuple
###### tags: `python`
> [name=Ice]
> [time=Thu, Nov 3, 2022 8:57 PM]
## [List](https://hackmd.io/tp9CAidmQQ6wlOXRTdSmvQ) vs. Tuple
* Tuple = **唯獨**的List
* 可互換：
```python=
L = [1,2,3] #list
T = (4,5,6) #tuple
L_To_T = tuple(L)
T_To_L = list(T)
print(L_To_T #(1, 2, 3)
print(T_To_L #[4, 5, 6]
```
## 元組建立
### 小括號包住值
```python=
a = ('zero', 'one', 'two')
print(a) #('zero', 'one', 'two')
```
### 拆字串轉元組tuple()
```python=
a='cute'
a=tuple(a)
print(a) #('c', 'u', 't', 'e')
```
## Tuple Method
### **index()** 查詢位址
查詢元素位址
```python=
a = ('a', 'b', 'c')
print(a.index('c')) #2
```
### **count()** 計算元素數量
計算指定元素數量
```python=
a = (1, 2, 3, 2, 2, 3, 5)
print(a.count(2)) #3
print(a.count(5)) #1
```