python入门到放弃-基本数据类型之tuple元组
guoke-boy 人气:4#概述
元组俗称不可变的列表,又称只读列表,是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字符串篇
加载全部内容