if语法:
类型一:
if expression : if_suit else: else_suit
例如:
adic={"name":"paulwinflo","age":18,"sex":"man"}
if "name" in adic:
print "hello "+ adic["name"]
else:
print "no this key "
类型二:
if expression :#条件一 if_suit
elif expression1 :#条件二
elif_suit
elif expression2 :#条件三
elif_suit
..
..
else: #其他情况下
else_suit
例如:
num=input("please input a number:")
if num > 0 :
print str(num) + " is above zero"
elif num < 0:
print str(num) + " is ablow zero"
else:
print str(num) + " is eqel zero"
while语法: while语句会不停地执行下去直到条件false或者语句体遇见break.
while expression:
while_suit
例如:
计数循环:通过变量的数值大小进行控制
num=input("please input a number:")
while num > 8 and num <13 :##符合条件的数进入while循环
print str(num)+" is ok"
num+=1
开关循环:通过变量的bool特性进行开关循环
control=False##循环控制器打开
name=raw_input("please input your name: ")
names=["paul","zhens","win"]
while not control:
if name not in names:
print "please input again"
control=True##循环控制器关闭
else:
control=True##循环控制器关闭
for语法: 主要功能还是遍历功能
for a in range(7):
print a,
综合使用实力----列表解析
alist=[x**2 for x in range(12)if not x%3]##因为前面前面有not,因此x%3需为false也就是0 print alist