demo4_元组

来源:互联网 发布:淘宝刷手要身份证照片 编辑:程序博客网 时间:2024/06/13 19:29
'''
Python的元组与列表类似,不同之处在于元组的元素不能修改
也可进行分片 和 连接操作. 元组使用小括号,列表使用方括号。


'''
print("====================访问元组=======================")


tuple=('hello',100,3.14)
print(tuple[0]+" %d"%tuple[1])


print('====================合并元组=======================')


tup1=('hello',100,3.14)
tup2=(1,2,3,4,5,6)
tup3=tup1+tup2
print(tup3)


print('====================判断长度=======================')
tup1=('hello',100,3.14)
print(len(tup1))


print("===================判断是否存在====================")


tup1=('hello',100,3.14)
print(3 in tup1)


print("=====================遍历===========================")


tup1=('hello',100,3.14)
for x in tup1:
print(x)


print("=========反向读取;读取倒数第二个元素===============")
tup1=('hello',100,3.14)
print(tup1[-2])


print("=====================截取元素========================")
tup1=('hello',100,3.14)
print(tup1[1:])


print("===================返回元组中元素最大值===============")
tup1=(2,100,3.14)

print(max(tup1))


print("===================返回列表转换元组===============")
A = ["zhangsan","lisi","wangwu"]

b=tuple(A)

print(b)
原创粉丝点击