python中元组相关操作

#创建空元组
#方式一:使用()
tuple_a=()
print(type(tuple_a))

#方式二:使用tuple()
tuple_b=tuple()
print(type(tuple_b))

#初始化元组
#set集合初始化元组
tuple_c=tuple({1,2,3,4})
print(type(tuple_c))
print(tuple_c)

#list列表初始化元组
tuple_d=tuple([1,2,3,4])
print(type(tuple_d))
print(tuple_d)

#读取元组
#方式一:使用下标的方式进行读取
tuple_d=tuple([1,2,3,4])
print(tuple_d[0])

#方式二:使用切片的方式进行读取
print(tuple_d[1:])
print(tuple_d[:])
print(tuple_d[::-1])

#更新元组中的元素
py_tuple = (22, 33, 55, 66, [88, 99])
#py_tuple[0]=77 #TypeError: ‘tuple’ object does not support item assignment

#元组中的列表是可以进行修改了
py_tuple[4][0]=77
print(py_tuple)

#元组进行合并
first_tuple = (‘p’, ‘y’, ‘t’)
second_tuple = (‘h’, ‘o’, ‘n’)

print(first_tuple+second_tuple)

#元组重复操作
init_tuple = (“fork”, )
fork_tuple = init_tuple * 5

print(fork_tuple)

#元组删除操作
first_tuple = (‘p’, ‘y’, ‘t’)
#不使用以下的方式进行删除元组中的元素
#del first_tuple[0] #TypeError: ‘tuple’ object doesn’t support item deletion
#只能使用以下的方式进行删除元组
del first_tuple

#遍历元给中的元素
item=tuple([1,2,3,4])
for i in item:
print(i)

#判断元组中是否存在某个值
print(1 in item)
#判断元组中是否不存在某个值
print(9 in item)

#变量元组
emp_records = (‘john’, ‘hr’, 2010, ‘robert’, ‘account’, 2015, ‘bill’, ‘mis’, 2018)
(a,b,c)=emp_records[0:3]
print(a)
print(b)
print©

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值