下面是一个非常有用的小教程基本上也就记住了 ,但是需要长时间复习
if句(判断语句)-------------------------------------------------------
i = 1
if i > 0:
print ‘positive i‘
i = i + 1
elif i == 0:
print ‘i is 0‘
i = i * 10
else:
print ‘negative i‘
i = i - 1
print ‘new i:‘,i
--------------------------------------------------for语句(循环)-----------------------------------------------------------
for i in range(10): # for 元素 in 序列:
print i**2 # 动作statement
result:1,4,9,16,25,36,49,64,81
------------------------------------------------- while 语句(循环)--------------------------------------------------------------
while 条件:
statement
举个例子:
>>> i = 5 # 给予变量i赋值
>>> while i < 10:
print i
i = i +1
result:
5
6
7
8
9
---------------------------------------------------------中断-continue---------------------------------------------------------------
>>> for i in range(10):
if i == 2:
continue #同一循环的某一环,continue,跳过这一环,进行下一环的操作
print i
result:
0
1
3
4
5
6
7
8
9
---------------------------------------------------------中断-break---------------------------------------------------------------
>>> for i in range(10):
if i == 2:
break #停止整个循环,不进行下一次循环
print i
result:
0
1