Python数据类型详解(三)元祖:tuple
一.基本数据类型
整数:int
字符串:str(注:\t等于一个tab键)
布尔值:bool
列表:list
列表用[]
元祖:tuple
元祖用()
字典:dict
注:所有的数据类型都存在想对应的类列里,元祖和列表功能一样,列表可以修改,元祖不能修改。
二.列表所有数据类型:
基本操作:
索引,切片,长度,包含,循环
classtuple(object): """ tuple()->emptytuple tuple(iterable)->tupleinitializedfromiterable'sitems Iftheargumentisatuple,thereturnvalueisthesameobject. """ defcount(self,value):#realsignatureunknown;restoredfrom__doc__ """T.count(value)->integer--returnnumberofoccurrencesofvalue""" (T.count(价值)->整数,返回值的出现次数) return0 defindex(self,value,start=None,stop=None):#realsignatureunknown;restoredfrom__doc__ """ T.index(value,[start,[stop]])->integer--returnfirstindexofvalue. RaisesValueErrorifthevalueisnotpresent. """ (T。指数(价值,[开始,[不要]])->整数,返回第一索引值。提出了ValueError如果不存在的价值。) return0 def__add__(self,*args,**kwargs):#realsignatureunknown """Returnself+value.""" pass def__contains__(self,*args,**kwargs):#realsignatureunknown """Returnkeyinself.""" pass def__eq__(self,*args,**kwargs):#realsignatureunknown """Returnself==value.""" pass def__getattribute__(self,*args,**kwargs):#realsignatureunknown """Returngetattr(self,name).""" pass def__getitem__(self,*args,**kwargs):#realsignatureunknown """Returnself[key].""" pass def__getnewargs__(self,*args,**kwargs):#realsignatureunknown pass def__ge__(self,*args,**kwargs):#realsignatureunknown """Returnself>=value.""" pass def__gt__(self,*args,**kwargs):#realsignatureunknown """Returnself>value.""" pass def__hash__(self,*args,**kwargs):#realsignatureunknown """Returnhash(self).""" pass def__init__(self,seq=()):#knownspecialcaseoftuple.__init__ """ tuple()->emptytuple tuple(iterable)->tupleinitializedfromiterable'sitems Iftheargumentisatuple,thereturnvalueisthesameobject. #(copiedfromclassdoc) """ pass def__iter__(self,*args,**kwargs):#realsignatureunknown """Implementiter(self).""" pass def__len__(self,*args,**kwargs):#realsignatureunknown """Returnlen(self).""" pass def__le__(self,*args,**kwargs):#realsignatureunknown """Returnself<=value.""" pass def__lt__(self,*args,**kwargs):#realsignatureunknown """Returnself<value.""" pass def__mul__(self,*args,**kwargs):#realsignatureunknown """Returnself*value.n""" pass @staticmethod#knowncaseof__new__ def__new__(*args,**kwargs):#realsignatureunknown """Createandreturnanewobject.Seehelp(type)foraccuratesignature.""" pass def__ne__(self,*args,**kwargs):#realsignatureunknown """Returnself!=value.""" pass def__repr__(self,*args,**kwargs):#realsignatureunknown """Returnrepr(self).""" pass def__rmul__(self,*args,**kwargs):#realsignatureunknown """Returnself*value.""" pass
三.所有元祖数据类型举例
#count用于计算元素出现的个数 name_tuple=("zhangyanlin","suoning","nick") print(name_tuple.count('zhangyanlin')) #index获取指定元素的指定位置 name_tuple=("zhangyanlin","suoning","nick") print(name_tuple.index('zhangyanlin'))
四.索引
name_tuple=("zhangyanlin","suoning","nick") print(name_tuple[1])
五.切片
#取出第一位到最后一位减1的元素 name_tuple=("zhangyanlin","suoning","nick") print(name_tuple[0:len(name_tuple)-1])
六.总长度len
#取出最后一位减1的元素 name_tuple=("zhangyanlin","suoning","nick") print(name_tuple[len(name_tuple)-1])
七.for循环
name_tuple=("zhangyanlin","suoning","nick") foriinname_tuple: print(i)
那么使用tuple有什么好处呢?
Tuple比list操作速度快。如果您定义了一个值的常量集,并且唯一要用它做的是不断地遍历它,请使用tuple代替list。
如果对不需要修改的数据进行“写保护”,可以使代码更安全。使用tuple而不是list如同拥有一个隐含的assert语句,说明这一数据是常量。如果必须要改变这些值,则需要执行tuple到list的转换(需要使用一个特殊的函数)。
还记得我说过dictionarykeys可以是字符串,整数和“其它几种类型”吗?Tuples就是这些类型之一。Tuples可以在dictionary中被用做key,但是list不行。实际上,事情要比这更复杂。Dictionarykey必须是不可变的。Tuple本身是不可改变的,但是如果您有一个list的tuple,那就认为是可变的了,用做dictionarykey就是不安全的。只有字符串、整数或其它对dictionary安全的tuple才可以用作dictionarykey。
Tuples可以用在字符串格式化中,我们会很快看到。