条件控制语句
if语句
Python中if语句的一般形式如下所示:
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
- 如果 “condition_1” 为 True 将执行 “statement_block_1” 块语句
- 如果 “condition_1” 为False,将判断 “condition_2”
- 如果"condition_2" 为 True 将执行 “statement_block_2” 块语句
- 如果 “condition_2” 为False,将执行"statement_block_3"块语句
- Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else。
注意:
- 每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。
- 使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
- 在Python中没有switch – case语句。
实例
以下实例演示了狗的年龄计算判断:
#!/usr/bin/python3
age = int(input("请输入你家狗狗的年龄: "))
print("")
if age <= 0:
print("你是在逗我吧!")
elif age == 1:
print("相当于 14 岁的人。")
elif age == 2:
print("相当于 22 岁的人。")
elif age > 2:
human = 22 + (age -2)*5
print("对应人类年龄: ", human)
### 退出提示
input("点击 enter 键退出")
if嵌套
在嵌套 if 语句中,可以把 if…elif…else 结构放在另外一个 if…elif…else 结构中
#! /usr/bin/python3
num = int(input("请输入一个数字:"))
if num%2 == 0:
if num%3 == 0:
print("你输入的数字可以整除 2 和 3 !")
else:
print("你输入的数字可以整除 2,但不能整除 3 !")
else:
if num%3 == 0:
print("你输入的数字可以整除 3,但不能整除 2 !")
else:
print("你输入的数字不能整除 2 和 3!")
效果展示
# python3 num.py
请输入一个数字:6
你输入的数字可以整除 2 和 3 !
使用判断语句来实现 BMI 的计算。
BMI 指数(即身体质量指数,简称体质指数又称体重,英文为 Body Mass Index,简称BMI),是用体重公斤数除以身高米数平方得出的数字
#! /usr/bin/python3
print("========欢迎进入BMI计算程序!========")
name = input("请输入你的姓名:")
height = eval(input("请输入您的身高(cm):"))
weight = eval(input("请输入您的体重(kg):"))
gender = input("请输入您的性别(F/M):")
# 公式
BMI = float(float(weight)/((float(height)/100)**2))
if BMI <= 18.4:
print("姓名:",name,"BMI值:",BMI,"身体状态:偏瘦")
elif BMI <= 23.9:
print("姓名:",name,"BMI值:",BMI,"身体状态:正常")
elif BMI <= 27.9:
print("姓名:",name,"BMI值:",BMI,"身体状态:超重")
elif BMI >= 28:
print("姓名:",name,"BMI值:",BMI,"身体状态:肥胖")
# time模块
import time;
nowtime = (time.asctime(time.localtime(time.time())))
if gender == "F":
print("感谢",name,"女士在",nowtime,"使用本程序,祝你身体健康!")
elif gender == "M":
print("感谢",name,"先生在",nowtime,"使用本程序,祝你身体健康!")
效果展示
循环语句
while 循环
Python 中 while 语句的一般形式:
while 判断条件(condition):
执行语句(statements)……
以下实例使用了 while 来计算 1 到 100 的总和:
#!/usr/bin/env python3
n = 100
sum = 0
counter = 1
while counter <= n:
sum = sum + counter
counter += 1
print("1 到 %d 之和为: %d" % (n,sum))
执行结果如下:
1 到 100 之和为: 5050
无限循环
我们可以通过设置条件表达式永远不为 false 来实现无限循环,实例如下:
#!/usr/bin/python3
var = 1
while var == 1 : # 表达式永远为 true
num = int(input("输入一个数字 :"))
print ("你输入的数字是: ", num)
print ("Good bye!")
执行以上脚本,输出结果如下:
输入一个数字 :5
你输入的数字是: 5
输入一个数字 :
你可以使用 CTRL+C 来退出当前的无限循环。
无限循环在服务器上客户端的实时请求非常有用。
while 循环使用 else 语句
在 while … else 在条件语句为 false 时执行 else 的语句块。
语法格式如下:
while <expr>:
<statement(s)>
else:
<additional_statement(s)>
循环输出数字,并判断大小:
实例
#!/usr/bin/python3
count = 0
while count < 5:
print (count, " 小于 5")
count = count + 1
else:
print (count, " 大于或等于 5")
执行以上脚本,输出结果如下:
0 小于 5
1 小于 5
2 小于 5
3 小于 5
4 小于 5
5 大于或等于 5
简单语句组
类似if语句的语法,如果你的while循环体中只有一条语句,你可以将该语句与while写在同一行中, 如下所示:
#!/usr/bin/python
flag = 1
while (flag): print ('hello world!')
print ("Good bye!")
**注意:**以上的无限循环你可以使用 CTRL+C 来中断循环。
执行以上脚本,输出结果如下:
hello world!
hello world!
hello world!
hello world!
......
for 语句
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
for循环的一般格式如下:
for <variable> in <sequence>:
<statements>
else:
<statements>
以下 for 实例中使用了 break 语句,break 语句用于跳出当前循环体:
实例
#!/usr/bin/python3
sites = ["Baidu", "Google","jingdong","Taobao"]
for site in sites:
if site == "jingdong":
print("京东!")
break
print("循环数据 " + site)
else:
print("没有循环数据!")
print("完成循环!")
执行脚本后,在循环到"jingdong"时会跳出循环体:
循环数据 Baidu
循环数据 Google
京东!
完成循环!