元组
类似列表(当成容器使用) 特点: 1、定义的符号() 2、元组中的内容不可修改 3、关键字:tuple
元组类型
t1=()
print(type(t1))
当元组里放一个元素时
```python
t2=(1)
print(type(t2))#如果元组值一一个元素,默认与没加括号一个意思
<class 'int'>
因此要加逗号,
t2=(1,)
print(type(t2))
元组放多个元素时
t2=('aa','bb')
print(type(t2))
元组的查询方法
举例说明:
import random
list=[]
for i in range(10):
ran=random.randint(1,20)
list.append(ran)
print(list)
tuple1=tuple(list)
print(tuple1)
print(tuple1[0])
print(tuple1[-1])
print(tuple1[1:3])
print(tuple1[1:-2])
print(tuple1[::-1])
print(max(tuple1))
print(min(tuple1))
print(sum(tuple1))
print(len(tuple1))
print(tuple1.count(4))
print(tuple1.index(1))
|