list.append(item)
list.extend(sequence)
http://docs.python.org/tutorial/datastructures.html
http://docs.python.org/library/functions.html 这几天看一下
python howto
恩。python documentation 确实很好很强大啊!
-
list.
append
(
x
)
- Add an item to the end of the list; equivalent to a[len(a):] = [x].
- a[len(a):] 是一个list,slice得到的一个list,相当于把这一段截取出来的list,要是等于一个list b,相当于在a的末尾添加了b,但是b整个是一个元素。
- >>> c.append([1,2,3])
- >>> c
- [1, 2, 3, 4, [1, 2, 3]]
-
list.
extend
(
L
)
- Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
-
- 添加L中的元素,延长a
For loop, for(i=0; i<n; i++) in python is for i in range(0, n)
对于步长为2,则 for i in range(0, n, 2): range 函数可以改变步长! 不用 i += 2, Mark一下。
for i in range(0,10,2):
print i
0
2
4
6
8
比如寻找素数的程序。那么,寻找是不是被整除的时候,除了2 之外,可以从3开始,步长为2的去搜索。直到[sqrt(prime_n)](就是int(sqrt(prime_n)), 高斯函数)为止。