我建议你学习的过程也按照上面来,首先过一遍python官方文档:
http://docs.python.org/2.7/tutorial/index.html
如何查找python的某个功能?
http://docs.python.org/2.7/library/index.html
C or C++ 扩展 read Extending and Embedding the Python Interpreter and Python/C API Reference Manual.
for语句也支持一个选用的else块,它的工作就像在while循环中一样:如果循环离开时没有碰到break语句,就会执行(也就是序列所有元素都被访问过了)
break和continue语句也可用在for循环中,就像while循环那样。for循环完整的格式如下:
for <target> in <object>:
<statements>
if <test>:break
if <test>:conitnue
else:
<statements>
[python] view plaincopy
a = ['a1', 'a2', 'a3']
b = ['b1', 'b2']
# will iterate 3 times,
# the last iteration, b will be None
print "Map:"
for x, y in map(None, a, b):
print x, y
# will iterate 2 times,
# the third value of a will not be used
print "Zip:"
for x, y in zip(a, b):
print x, y
# will iterate 6 times,
# it will iterate over each b, for each a
# producing a slightly different outpu
print "List:"
for x, y in [(x,y) for x in a for y in b]:
print x, y
[python] view plaincopy
#demo for 'for'
# -*- coding: cp936 -*-
#for in
for i in range(1,5):
print i
#step 2
for i in range(1,5,2):
print i;
#break
for i in range(1,5):
if(i==6):
break
else:
print 'break hello'
#求质数
import math
for i in range(50, 100 + 1):
for j in range(2, int(math.sqrt(i)) + 1):
if i % j == 0:
break
else:
print i
#continue
for i in range(1,5):
if(i==4):
continue
print 'no met continue'
else:
print i
5.while 语句
while循环的一般格式如下:
while <test>:
<statements1>
if <test2>:break
if <test3>:continue
if <test4>:pass
else:
<statements2>
break和continue可以出现在while(或for)循环主体的任何地方,但通常会进一步嵌套在if语句中,根据某些条件来采取对应的操作。