腾讯课堂——基础数据类型(tuple元祖)

Posted l-hf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了腾讯课堂——基础数据类型(tuple元祖)相关的知识,希望对你有一定的参考价值。

初识元祖

标准类型
数字
字符串
列表
字典
元组

 

 

 

 

 

 

 

   定义:与列表类似,只不过[]改成()

  特性:

    1.可存放多个值
    2.不可变
    3.
按照从左到右的顺序定义元组元素,下标从0开始顺序访问,有序

元祖的创建

  ages = (11, 22, 33, 44, 55)
  或
  ages = tuple((11, 22, 33, 44, 55))
  顺序及下标图示

技术图片

元祖的常用操作

  索引

  切片  

  循环

  长度

  包含

元祖的特性详解

  1.可存放多个值

    如果元祖中只有一个值

t = (1,)
t = (1)   #<==>t = 1

    元祖中不仅可以存放数字、字符串,还可以存放更加复杂的数据类型

  2.不可变 

    元祖本身不可变,如果元祖中还包含其他可变元素,这些可变元素可以改变

元祖相关知识拾遗

  字典中遇到的tuple

   1.字典的定义

    元祖可以作为字典的key:

dic = (alex,male):v1,(wusir,male):v2
print(dic)

     字典定义可以用到元祖:

person = dict(([name,苑昊],[文周,18]))
person = dict(((name,苑昊),(文周,18)))
print(person)

   2.字典的items方法,将字典中的每一项作为元祖返回

dic = k1:v1,k2:v2
print(dic.items())
#dict_items([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘)])

    再讲循环

dic = key1:value1,key2:value2
for key,value in dic.items():
    print(key,value)

 

元祖的工厂函数

技术图片
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元祖)的主要内容,如果未能解决你的问题,请参考以下文章

python基础数据类型tuple

Python三种基础数据类型:列表list,元祖tuple和字典dict

Python 基础数据类型之tuplu

高级数据类型---元祖[tuple]

04. 万恶之源-基本数据类型(list,tuple)

Python基础数据类型