标签:lin type argument span round 浮点数 app style one
整数
浮点数
字符串
对象
创建列表
方式一:创建一个普通列表
>>>member = [‘jhon‘, ‘dog‘, ‘pig‘ ]
>>>member
[‘jhon‘, ‘dog‘, ‘pig‘ ]
方式二: 创建一个混合列表
>>>mix = [1, ‘fish‘, 3.14, [1, 2, 3]]
方式三:创建一个空列表
>>> empty = []
向列表添加元素:
append()
>>> member.append(‘cat‘)
>>> member
[‘jhon‘, ‘dog‘, ‘pig‘, ‘cat‘]
>>> len(member)
4
>>>member.append(‘fish‘, ‘heike‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: append() takes exactly one argument (2 given)
extend()
>>> member.extend(‘fish‘,‘heike‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extend() takes exactly one argument (2 given)
>>> member.extend([‘fish‘,‘heike‘])
>>> member
[‘jhon‘, ‘dog‘, ‘pig‘, ‘cat‘, ‘fish‘, ‘heike‘]
insert()
>>> member.insert(1,‘中关村‘)
>>> member
[‘jhon‘, ‘中关村‘, ‘dog‘, ‘pig‘, ‘cat‘, ‘fish‘, ‘heike‘]
从列表中获取元素
跟数组一样,我们可以通过元素的索引直(index)从列表获取单个元素,注意,列表索引从0开始
>>> member[0]
‘jhon‘
交换元素位置
>>> temp = member[0]
>>> member[0] = member[1]
>>> member[1] = temp
>>> member
[‘中关村‘, ‘jhon‘, ‘dog‘, ‘pig‘, ‘cat‘, ‘fish‘, ‘heike‘]
标签:lin type argument span round 浮点数 app style one
原文地址:http://www.cnblogs.com/fengjunjie-w/p/7460784.html