Python大法_________元组

元组的创建

tup = (1, 2.2, True, ‘hell’)    # 可以放任意类型的数据
print(tup)
print(type(tup))
ret = tup[1]
print(ret)

切片访问

tup1 = tup[0:4:1]
print(tup1)

使用多种姿势 接收元组的值

name, age, height, weight = ('zs', 12, 123, 2134)
print('姓名是', name)
print('年龄是', age)
print('身高是', height)
print('体重是', weight)

biao = (('zs', 10), ('sd', 19), ('aa', 20))
for i in biao:
    print(i)
    for info in i:
        print(info)

for name, age in biao:
    print(name, age)

Python大法_________元组_第1张图片

# tuple 和 ist()函数

lst=[1,2,3]
tup=tuple(lst)
print(tup)
tup=(‘a’,‘b’,‘c’)
ret=list(tup)
print(ret)
Python大法_________元组_第2张图片

——————————————————————————————————————————

元组的方法

tup = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’)
ret = tup.index(‘b’) # 根据内容反对应索引
ret = tup.index(‘d’, 2, len(tup)) #得出结果3
print(ret)

你可能感兴趣的:(Python大法_________元组)