1、列表是一种
可修改的集合类型,其元素可以是数字、string等基本类型,也可以是列表、元组、字典等集合对象,甚至可以是自定义的类型。其定义方式如下:
>>> nums = [1,2,3,4]
>>> type(nums)
<type 'list'>
>>> print nums
[1, 2, 3, 4]
>>> strs = ["hello","world"]
>>> print strs
['hello', 'world']
>>> lst = [1,"hello",False,nums,strs]
>>> type(lst)
<type 'list'>
>>> print lst
[1, 'hello', False, [1, 2, 3, 4], ['hello', 'world']]
2、用索引的方式访问列表元素, 索引从0开始,支持负数索引,-1为最后一个:
>>> lst = [1,2,3,4,5]
>>> print lst[0]
1
>>> print lst[-1]
5
>>>
>>> nums = [1,2,3,4]
>>> type(nums)
<type 'list'>
>>> print nums
[1, 2, 3, 4]
>>> strs = ["hello","world"]
>>> print strs
['hello', 'world']
>>> lst = [1,"hello",False,nums,strs]
>>> type(lst)
<type 'list'>
>>> print lst
[1, 'hello', False, [1, 2, 3, 4], ['hello', 'world']]
2、用索引的方式访问列表元素, 索引从0开始,支持负数索引,-1为最后一个:
>>> lst = [1,2,3,4,5]
>>> print lst[0]
1
>>> print lst[-1]
5
>>>