Python中元组的使用方法

Python中元组的使用方法

创建元组

tuple = (1, 2, 3, 4, 5)
tuple2 = (6, 7, 8, 9, 0)
print("tuple元组中的元素为:", tuple)
print("tuple2元组中的元素为:", tuple2)
print("-----------------------------------")

访问元组

print("tuple[0]:", tuple[0])  # tuple1元组中第一个元素
print("tuple2[2]:", tuple2[2])  # tuple2元组中第三个元素
print("------------------------------------")

修改元组

tuple = (1, 2, 3, 4, 5, 6)
print("修改后的tuple1元组为:", tuple)
print("------------------------------------")

删除元组

'''
    因为元组为不可变数据类型,所以不能删除,只要删除就会报错。
'''

元组运算符

1.计算元组个数

num = len(tuple2)
print("tuple2元组元素个数为:", num)

2.连接元组

tuple3 = tuple2 + tuple  # tuple1 + tuple2 与 tuple2 + tuple1
print("新组成的tuple3元组中元素为:", tuple3)

3.复制元组

tuple4 = ('Hi') * 4
print("tuple4元组的元素为:", tuple4)

4.查看元素是否存在

judge = 3 in (1, 2, 3) #judge判断的意思
print(judge)
print("-------------------------------------")

5.迭代

# 方法一:
for i in tuple:
    print(i, end=' ')
# 方法二:
it = iter(tuple4)
print(next(it))
'''
注意:tuple中没有‘__next__'属性,不能使用'__next__',
否则会报如下错误:AttributeError: 'tuple' object has no attribute '__next__'
例:
a = ('csd', 'dwe', 'DWE')
it = iter(a)
print(a.__next__())
'''

6.元组的索引、截取

a = tuple4[2]
print(a)
print(tuple3[2])

7. 元组内置函数

a = len(tuple3)  # tuple3元组长度
print(a)

max = max(tuple3) # tuple3元组中最大元素值
print(max)

min = max(tuple3) # tuple3元组中最小元素值
print(min)

结果:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值