元組的 max min sum

  1. 串列可以修改,元組不能變更,所以應用在串列的 method 或 function,只要不會變更元組內容的都可以使用。
    [dywang@deyu zzz]$ cat tuple4.py 
    #!/usr/bin/python3
    # coding: utf-8
    
    tuplex = ( 32, 12, -98 )
    print("len(tuplex) =", len(tuplex))
    print("max(tuplex) =", max(tuplex))
    print("min(tuplex) =", min(tuplex))
    print("sum(tuplex) =", sum(tuplex))
    tupley = ( '9', 65, 54, 21, '7' )
    print("len(tupley) =", len(tupley))
    print("max(tupley) =", max(tupley))
    print("min(tupley) =", min(tupley))
    print("sum(tupley) =", sum(tupley))
    
  2. 執行結果:len, max, min sum 對所有元素都是整數的元組可用,但 max min sum 在有字元及整數的元組不可用。
    [dywang@deyu zzz]$ ./tuple4.py 
    len(tuplex) = 3
    max(tuplex) = 32
    min(tuplex) = -98
    sum(tuplex) = -54
    max(tupley) = 5
    Traceback (most recent call last):
      File "./tuple4.py", line 11, in <module>
        print("max(tupley) =", max(tupley))
    TypeError: '>' not supported between instances of 'int' and 'str'