# Python学习笔记（六）—— 序列运算

``>>> l=[1,2,3]>>> l*5``
``[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]``
``>>> 5*'abcd'``
``'abcdabcdabcdabcdabcd'``

``>>> board=[['_']*3 for i in range(3)]>>> board``
``[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]``
``>>> board[1][1]='X'>>> board``
``[['_', '_', '_'], ['_', 'X', '_'], ['_', '_', '_']]``

``>>> weird_board=[['_']*3]*3>>> weird_board``
``[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]``
``>>> weird_board[1][1]='Y'>>> weird_board``
``[['_', 'Y', '_'], ['_', 'Y', '_'], ['_', 'Y', '_']]``

``>>> row=['_']*3>>> board=[]>>> for i in range(3):... board.append(row)``

``>>> board=[]>>> for i in range(3):... row=['_']*3... board.append(row)>>> board``
``[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]``
``>>> board[1][1]='Y'>>> board``
``[['_', '_', '_'], ['_', 'Y', '_'], ['_', '_', '_']]``

``>>> l=[1,2,3]>>> id(l)``
``2297732364616``
``>>> l*=2>>> l``
``[1, 2, 3, 1, 2, 3]``
``>>> id(l)``
``2297732364616``
``>>> t=(1,2,3)>>> id(t)``
``2297732591552``
``>>> t*=2>>> t``
``(1, 2, 3, 1, 2, 3)``
``>>> id(t)``
``2297732664584``

``t=(1,2,[30,40])t[2]+=[50,60]``

a. t变为(1, 2, [30, 40, 50, 60])
b. TypeError: ‘tuple’ object does not support item assignment
c. a和b都不对
d. a和b都对

``>>> t=(1,2,[30,40])>>> t[2]+=[50,60]``
``---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-19-a113d2524452> in <module>() 1 t=(1,2,[30,40])----> 2 t[2]+=[50,60]TypeError: 'tuple' object does not support item assignment``
``>>> t``
``(1, 2, [30, 40, 50, 60])``

``>>> import dis>>> dis.dis('S[a]+=b')``
`` 1 0 LOAD_NAME 0 (S) 2 LOAD_NAME 1 (a) 4 DUP_TOP_TWO 6 BINARY_SUBSCR 8 LOAD_NAME 2 (b) 10 INPLACE_ADD 12 ROT_THREE 14 STORE_SUBSCR 16 LOAD_CONST 0 (None) 18 RETURN_VALUE``
• “BINARY_SUBSCR” 将S[a]的值置于栈顶（Top Of Stack,TOS）