专栏首页用户7095785的专栏python入门到放弃(六)-基本数据类型之tuple元组

python入门到放弃(六)-基本数据类型之tuple元组

#概述

元组俗称不可变的列表,又称只读列表,是python的基本数据类型之一, 用()小括号表示,里面使用,逗号隔开

元组里面可以放任何的数据类型的数据,查询可以,循环可以,但是就是不能修改

#先来看看tuple元组的源码写了什么,方法:按ctrl+鼠标左键点tuple

lass tuple(object):
    """
    tuple() -> empty tuple
    tuple(iterable) -> tuple initialized from iterable's items
    
    If the argument is a tuple, the return value is the same object.
    """
    def count(self, value): # real signature unknown; restored from __doc__
        """ T.count(value) -> integer -- return number of occurrences of value """
        return 0

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
        """
        T.index(value, [start, [stop]]) -> integer -- return first index of value.
        Raises ValueError if the value is not present.
        """
        return 0

    def __add__(self, y): # real signature unknown; restored from __doc__
        """ x.__add__(y) <==> x+y """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x """
        pass

    def __eq__(self, y): # real signature unknown; restored from __doc__
        """ x.__eq__(y) <==> x==y """
        pass

    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        pass

    def __getslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__getslice__(i, j) <==> x[i:j]
                   
                   Use of negative indices is not supported.
        """
        pass

    def __ge__(self, y): # real signature unknown; restored from __doc__
        """ x.__ge__(y) <==> x>=y """
        pass

    def __gt__(self, y): # real signature unknown; restored from __doc__
        """ x.__gt__(y) <==> x>y """
        pass

    def __hash__(self): # real signature unknown; restored from __doc__
        """ x.__hash__() <==> hash(x) """
        pass

    def __init__(self, seq=()): # known special case of tuple.__init__
        """
        tuple() -> empty tuple
        tuple(iterable) -> tuple initialized from iterable's items
        
        If the argument is a tuple, the return value is the same object.
        # (copied from class doc)
        """
        pass

    def __iter__(self): # real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass

    def __len__(self): # real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass

    def __le__(self, y): # real signature unknown; restored from __doc__
        """ x.__le__(y) <==> x<=y """
        pass

    def __lt__(self, y): # real signature unknown; restored from __doc__
        """ x.__lt__(y) <==> x<y """
        pass

    def __mul__(self, n): # real signature unknown; restored from __doc__
        """ x.__mul__(n) <==> x*n """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __ne__(self, y): # real signature unknown; restored from __doc__
        """ x.__ne__(y) <==> x!=y """
        pass

    def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

    def __rmul__(self, n): # real signature unknown; restored from __doc__
        """ x.__rmul__(n) <==> n*x """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ T.__sizeof__() -- size of T in memory, in bytes """
        pass

tuple

#判断是否是元组

print((3))   #不是元组
tu = (3, )   #元组中如果只有一个元素,需要在括号里写一个,逗号,否则就不是元组
tu = tuple() #空元组写法
print(type(tu))  #<class 'tuple'>

#验证元组不能增删改,查看索引就可以

tu = ("人民币","美元","美金","欧元")

tu.append("张三") #不允许添加:tuple' object has no attribute 'append'

tu[0] = "日元"    #不允许修改 :object does not support item assignment

del tu[2]        #报错,不允许删除:'tuple' object doesn't support item deletion

print(tu[2]) #索引就可以
#欧元

#关于元组不可变的注意点

这里元组的不可变意思是子元素不可变,但是子元素内容的子元素是可以变的,这要取决子元素本身是否是可变

#例子:

# 因为列表是可变长类型,所以可以在里面增加 tu = (1,"蒋小鱼","鲁炎",[]) tu[3].append("张冲") print(tu)
# (1, '蒋小鱼', '鲁炎', ['张冲'])

#需要注意的是,元组的第一层是不能进行赋值的,内部元素是没有要求的
#例子
#tu = (1,"巴朗","项羽","张飞") #像这样子是不能进行赋值的

#元组的索引和切片

tu = (3,4,5,6,7,8)
#索引:下标从0开始找
print(tu[0]) #3
print(tu[1]) #4
#切片
print(tu[1:3]) #(4, 5)

#详细使用可参考str字符串篇

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • python入门到放弃(七)-基本数据类型之dcit字典

    老油条IT记
  • python入门到放弃(八)-基本数据类型之set集合

    #注意点1:集合中的元素是不重复的,且无序的,所以打印出来的元素位置可能会和定义的位置不一样

    老油条IT记
  • python入门到放弃(四)-基本数据类型之str字符串

    老油条IT记
  • Python学习:作用域

    变量名引用分为三个作用域进行查找:首先是本地,之后是函数内(如果有的话),之后全局,最后是内置。L->E->G->B

    py3study
  • Scala第四章学习笔记(面向对象编程)

    DelayedInit特质是为编译器提供的标记性的特质。整个构造器被包装成一个函数并传递给delayedInit方法。

    用户3003813
  • photoshop 选框和套索工具

    上一篇文章photoshop常用图片处理技巧中,我大概介绍了一些基本概念。本篇章就是再细化讲解一下。

    Devops海洋的渔夫
  • Java+PHP整合

    验证两种各自领域称王的语言(JAVA /PHP),不同语言、不同机制的组合在一起,PHP负责WEB层,Java负责业务和数据逻辑层,真是一对黄金组合(Java+...

    梦_之_旅
  • WebSphere XXE 漏洞分析(CVE-2020-4643)

    作者:Longofo@知道创宇404实验室 & r00t4dm@奇安信A-TEAM 时间:2020年9月22日

    Seebug漏洞平台
  • 第四次文章:其他容器类

    各位小伙伴儿大家好,临近年关,先给大家拜个早年,祝大家新年快乐!今天先分享一碗鸡汤:代码学习绝对不是一条坦途,人生奋斗的向上之路也异常曲折,但没有关系,当你回望...

    鹏-程-万-里

扫码关注云+社区

领取腾讯云代金券