三大结构:顺序,分支,循环
分支
格式:
if 条件表达式:
语句1
...
elif 条件表达式:
语句1
...
elif 条件表达式:
...
...
(else:
语句1
...)
条件表达式就是计算结果必须为布尔值的表达式
表达式后的冒号绝对不能少!!!
例
1.#如果age小于18,则输出“不允许进入!如果age大于等于18,则输出“可以进入””
if age<18:
print ("不允许进入")
else:
print("可以进入")
2.分数判断
score = input("请输入学生成绩")
if score > 90:
print("EXCELLENT")
elif score >=80:
pinrt("GREAT")
elif score >=70:
print("GOOD")
elif score >=60:
print("OK")
else:
print("BAD")
注意:if不推荐嵌套使用,且Python中没有switch-case语句!
循环
重复执行固定语句
分别有:for循环和while循环
for循环
格式:
for 变量 in :
语句1
语句2
......
3.打印每个人名
for name in ['Kimmy','Lucy','Lily','Nancy']:
print(name,end=' ')
if name =='Lucy':
print("I LOVE {0}!".format(name),end = " ")
else:
print(",,,OH,NO",end=" ")
输出结果:
Kimmy ,,,OH,NO Lucy I LOVE Lucy! Lily ,,,OH,NO Nancy ,,,OH,NO
一些乱七八糟的是我结合前面所学加上去的,就当复习了。。。
while循环
while 条件表达式:
语句1
语句2
......
else:
语句1
语句2
......
例:
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
print "Good bye!"
输出结果:
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
其他流程控制语句
break,continue, pass: