前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python入门到放弃(六)-基本数据类型之tuple元组

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

作者头像
老油条IT记
发布2020-03-20 21:08:18
5130
发布2020-03-20 21:08:18
举报
文章被收录于专栏:用户7095785的专栏

#概述

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

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

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

代码语言:javascript
复制
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

#判断是否是元组

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

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

代码语言:javascript
复制
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]) #索引就可以
#欧元

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

代码语言:javascript
复制
这里元组的不可变意思是子元素不可变,但是子元素内容的子元素是可以变的,这要取决子元素本身是否是可变

#例子:

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

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

#元组的索引和切片

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-03-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档