list 與 tuple 比較

  1. list 與 tuple 變更元素內容比較。
    [dywang@deyu zzz]$ cat tuple2.py
    #!/usr/bin/python3
    # coding: utf-8
    
    listx = [ 32, 12, 'abc' ]
    tuplex = ( 32, 12, 'abc' )
    listx[1] = 10
    tuplex[1] = 10
    
  2. 執行結果:list 可以變更元素內容,tuple 變更元素內容會出現錯誤。
    [dywang@deyu zzz]$ ./tuple2.py 
    Traceback (most recent call last):
      File "./tuple2.py", line 7, in <module>
        tuplex[1] = 10
    TypeError: 'tuple' object does not support item assignment