python 笔记 for loop and extend, append

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)), 高斯函数)为止。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值