1.创建和访问一个元组

1.1创建元组

元组的标志性符号是 ,

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
#定义单个元素的元组,要加 ,
tuple2 = (1,)
tuple3 = 1, 2, 3
tuple4 = (1)
#定义一个空元组
tuple5 = ()
print(type(tuple1),type(tuple2),type(tuple3),type(tuple4),type(tuple5))

小甲鱼-013元组tuple:上了枷锁的列表-LMLPHP
### 1.2元组一旦定义,就不能更改。
```#python
tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(tuple1[1])
tuple1[1]=75
```
小甲鱼-013元组tuple:上了枷锁的列表-LMLPHP
## 2元组和列表的操作相似
```#python
tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(tuple1[1])
print(type(tuple1))
print(tuple1[:])
print(tuple1[1:5])
```
小甲鱼-013元组tuple:上了枷锁的列表-LMLPHP

# * 重复操作符复制元组
tuple1 = (8,)
print(tuple1 * 8)

小甲鱼-013元组tuple:上了枷锁的列表-LMLPHP

3.更新和删除一个元组

tuple1 = ('大娃', '二娃', '三娃', '四娃', '五娃', '六娃', '七娃')
#更新元组,实际是组装生成一个新元组,并将标签指向新元组。原来的旧元组在没有标签指向它的时候会被python的垃圾回收机制清理掉
tuple1 = tuple1[:2] + ('爷爷',) + tuple1[2:]
print(tuple1)
#删除元组
del tuple1

小甲鱼-013元组tuple:上了枷锁的列表-LMLPHP

05-10 23:01