python入門到放棄(六)-基本數據類型之tuple元組

 #概述python

元組俗稱不可變的列表,又稱只讀列表,是python的基本數據類型之一, 用()小括號表示,裏面使用,逗號隔開

元組裏面能夠聽任何的數據類型的數據,查詢能夠,循環能夠,可是就是不能修改

 

#先來看看tuple元組的源碼寫了什麼,方法:按ctrl+鼠標左鍵點tupleapp

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
tuple

 

#判斷是不是元組ide

print((3))   #不是元組
tu = (3, )   #元組中若是隻有一個元素,須要在括號裏寫一個,逗號,不然就不是元組
tu = tuple() #空元組寫法
print(type(tu))  #<class 'tuple'>

 

#驗證元組不能增刪改,查看索引就能夠spa

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]) #索引就能夠
#歐元

 

#關於元組不可變的注意點rest

這裏元組的不可變意思是子元素不可變,可是子元素內容的子元素是能夠變的,這要取決子元素自己是不是可變

#例子:code

# 由於列表是可變長類型,因此能夠在裏面增長 tu = (1,"蔣小魚","魯炎",[]) tu[3].append("張衝") print(tu)
# (1, '蔣小魚', '魯炎', ['張衝'])

#須要注意的是,元組的第一層是不能進行賦值的,內部元素是沒有要求的
#例子
#tu = (1,"巴朗","項羽","張飛") #像這樣子是不能進行賦值的

 

#元組的索引和切片blog

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

#詳細使用可參考str字符串篇索引

相關文章
相關標籤/搜索