list和tuple

list

>>> classmates = ['Michael','Bob','Tracy']
>>> classmates
['Michael', 'Bob', 'Tracy']
>>> len(classmates)
3
>>> classmates[0]
'Michael'
>>> classmates[-1]
'Tracy'
>>> classmates.append('Adam')
>>> classmates
['Michael', 'Bob', 'Tracy', 'Adam']
>>> classmates.insert(1,'Jack')
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
>>> classmates.pop()
'Adam'
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy']
>>> classmates.pop(1)
'Jack'
>>> classmates
['Michael', 'Bob', 'Tracy']
>>> classmates[1]='Sarah'
>>> classmates
['Michael', 'Sarah', 'Tracy']
>>> s=['python','java',['asp','php'],'scheme']
>>> s[2][0]
'asp'

tuple:一旦初始化,不能修改,在定义时tuple元素必须确定下来

>>> classmates = ('Michael','Bob','Tracy')
>>> classmates[-1]
'Tracy'
>>> t=(1,2)
>>> t
(1, 2)
>>> t=()
>>> t
()
>>> t=(1)
>>> t
1  #默认进行计算
>>>t=(1,)
>>>t
(1,)
>>> t=('a','b',['A','B'])
>>> t[2][0]='X'
>>> t
('a', 'b', ['X', 'B'])

你可能感兴趣的:(list和tuple)