Python基础(6):基本数据类型(tuple)

元组(tuple):  

  说明:元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。

  示例:

    name_tuple1 = ('physics', 'chemistry', 1997, 2000)

    name_tuple2 = (1, 2, 3, 4, 5 )

    name_tuple3 = "a", "b", "c", "d"

基本操作:

  • 索引
  • 切片
  • 循环
  • 长度
  • 包含
class 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, *args, **kwargs): # real signature unknown
        """ Return self+value. """
        pass

    def __contains__(self, *args, **kwargs): # real signature unknown
        """ Return key in self. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __getitem__(self, *args, **kwargs): # real signature unknown
        """ Return self[key]. """
        pass

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

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs): # real signature unknown
        """ Return hash(self). """
        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, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    def __mul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value.n """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __rmul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value. """
        pass
tuple

常用功能:

索引示例:

name_tuple = ('one','two')
print(name_tuple[0])
print(name_tuple[1])

运行结果:
one
two

 

len()示例:

name_tuple = ('one','two')
print(name_tuple[len(name_tuple)-1])

运行结果:
two

 

切片示例:

name_tuple = ('one','two')
print(name_tuple[0:1])

运行结果:
('one',)

 

for循环示例:

name_tuple = ('one','two')
for t in name_tuple:
    print(t)

运行结果:
one
two

del不支持(不可修改)。

 

元组内部提供的其他功能:

count(self, value):

  说明:计算元组中某个元素出现的次数。value:要统计的元素。(返回int)

  示例:

name_tuple = ('one','two','one')
n = name_tuple.count('one')
print(n)

运行结果:
2

 

index(self, value, start=None, stop=None):

  说明:从元组中找出某个值第一个匹配项的索引位置。value:查找匹配项。start:开始位置(从0开始),默认为无。end:结束位置,默认为无。(返回int)

  示例:

name_tuple = ('one','two','one')
i = name_tuple.index('one')
print(i)

运行结果:
0

 

转载于:https://www.cnblogs.com/niruing/p/7686838.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值