python语言之列表和元组的区别

    科技2022-07-21  91

    tuple的官方解释:

    https://docs.python.org/3/library/stdtypes.html?highlight=tuple#tuples

    Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the 2-tuples produced by the enumerate() built-in). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dict instance).

    关键词:存储异构的数据,不可变的序列

    #Author:jinhoward #Date:2020-10-04 tupleExample = ('Howard','36','XinYangNormalUniversity','60kg') aListExample = ['a','b',123,567,'hello','中国梦','鼠年大吉'] tupleExample[1] =35

    执行上述程序后报错如下:元组对象不支持元素赋值,这是因为元组是不支持修改的特性导致。

    Traceback (most recent call last): File "D:\pythonDebugging\tupleExample.py", line 6, in <module> tupleExample[1] =35 TypeError: 'tuple' object does not support item assignment

     

    Processed: 0.010, SQL: 8