python元组整合-5


关于列表方面请看: https://blog.csdn.net/weixin_41736752/article/details/96437875

2.元组

2.1特点

  • 有序
  • 可重复
  • 不可更改
  • 符号使用()

2.2 元组的创建

  • 空元组的创建,不能添加元素
  • 单元素元组的创建,需要在后面加逗号
>>> a=("a")
>>> type(a)
<class 'str'>

>>> a=('a',)
>>> type(a)
<class 'tuple'>
  • 多元素元组的创建,包含多种数据类型

2.3拼接、重复、索引(偏移)、切片

同列表一样,不做详细测试
https://blog.csdn.net/weixin_41736752/article/details/96437875

2.4增删改查

  • 索引查
  • 切片查
  • index() 返回位置

  • 只能全部删除
  • del 元组名即可

  • 不能改元组,但是可以改元组中的列表

    tp=('a','b','c',['a','b','c'])
    tp[3][1]='nice'
    #('a', 'b', 'c', ['a', 'nice', 'c'])
    

2.5基本函数

  • 最大值最小值

    max(tp) min(tp)

    tp=(1,5,9,4,5,10)
    print(max(tp),min(tp))
    #10 1
    

2.6遍历

  • 元素遍历

    tp=('one','two','and','a','b')
    for i in tp:
        print(i,end=' ')
    
  • 索引遍历

    tp=('one','two','and','a','b')
    for i in range(len(tp)):
        print(tp[i],end=' ')
    
  • 枚举enumerate

    tp=('one','two','and','a','b')
    for i in enumerate(tp,1):
        print(i,end=' ')
    #(1, 'one') (2, 'two') (3, 'and') (4, 'a') (5, 'b')
    

复习总结(字符串、列表、元组)

字符串、列表、元组的相同点和不同点

符号引号“ ”方括号[ ]小括号()
特征字符串列表元组
类型stringlisttuple
顺序\有序有序
重复、索引、切片TTT
Tappend()、extend()、insert(位置,元素)F
s.strip(“指定字符”)pop()、remove()、del、clear()del
s.replace([现有],[替新],替换数量)TF
s.find()、s.index()、s.rindex()、Tindex()
特点简单易操作拷贝注意三种情况不能对创建好的元组操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值