程序的分支结构
单分支结构
根据判断条件结果而选择不同向前路径的运行方式
guess = eval(input('please guess a number: '))
if guess == 99:
print('bingo, you got it')
print('want to continue?')
二分支结构
根据判断条件结果而选择不同向前路径的运行方式
guess = eval(input('please guess a number: '))
if guess == 99:
print('bingo, you got it')
else:
print('better luck next time.')
print('want to continue?')
紧凑形式:适用于简单表达式的二分支结构
guess = eval(input('please guess a number: '))
print("猜{}了".format("对" if guess==99 else "错"))
多分支结构
score = eval(input('please give your score: '))
grade = ''
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print('your grade is {} in this exam'.format(grade))
score = eval(input('please give your score: '))
grade = ''
if score >= 60:
grade = "D"
elif score >= 70:
grade = "C"
elif score >= 80:
grade = "B"
elif score >= 90:
grade = "A"
else:
grade = "F"
print('your grade is {} in this exam'.format(grade))
条件判断及组合
| **操作符** | **数学符号** | **描述** |
| —- | —- | —- | | < | < | 小于 | | <= | ≤ | 小于等于 | | >= | ≥ | 大于等于 | | > | > | 大于 | | == | = | 等于 | | != | ≠ | 不等于 |
用于条件组合的三个保留字
操作符及使用 | 描述 |
---|---|
x and y | 两个条件x和y的逻辑与 |
x or y | 两个条件x和y的逻辑或 |
not x | 条件x的逻辑非 |
guess = eval(input('please guess a number: '))
if guess > 99 or guess < 99:
print('better luck next time.'
else:
print('bingo, you got it')
程序的异常处理
num = eval(input("请输入一个整数: "))
print(num**2)
当用户没有输入整数时,会产生异常,怎么处理?
实例:身体质量指数BMI
BMI (Body Mass Index)
- 对身体质量的刻画
- 国际上常用的衡量人体肥胖和健康程度的重要标准,主要用于统计分析
- BMI = 体重 (kg) / 身高 (m)
- 体重 72 kg,身高 1.75 m,计算 BMI 值
国际:世界卫生组织
国内:国家卫生健康委员会
| **分类** | **国际BMI值 (kg/m2)** | **国内BMI值 (kg/m2)** |
| —- | —- | —- | | 偏瘦 | <18.5 | <18.5 | | 正常 | 18.5 ~ 25 | 18.5 ~ 24 | | 偏胖 | 25 ~ 30 | 24 ~ 28 | | 肥胖 | ≥30 | ≥28 |
问题分析
输入:给定体重和身高值
输出:BMI指标分类信息(国际和国内)
| **BMI值 (kg/m2)** | **国际分类** | **国内分类** |
| —- | —- | —- | | <18.5 | 偏瘦 | 偏瘦 | | 18.5 ~ 24 | 正常 | 正常 | | 24 ~ 25 | 正常 | 偏胖 | | 25 ~ 28 | 偏胖 | 偏胖 | | 28 ~ 30 | 偏胖 | 肥胖 | | ≥30 | 肥胖 | 肥胖 |
程序的循环结构
遍历循环
遍历某个结构形成的循环运行方式
- 从遍历结构中逐一提取元素,放在循环变量中
- 由保留字for和in组成,完整遍历所有元素后结束
- 每次循环,所获得元素放入循环变量,并执行一次语句块 ```python for i in range(100): print(i)
colors = [‘red’, ‘black’, ‘blue’, ‘gray’, ‘orange’] for color in colors: print(color)
word = ‘Astronomical Algorithms’ for c in word: print(c)
<a name="YHHo9"></a>
## 无限循环
**由条件控制的循环运行方式**
- 反复执行语句块,直到条件不满足时结束
```python
a = 3
while a > 0:
print(a)
a = a - 1
循环控制保留字
break 和 continue
break
跳出并结束当前整个循环,执行循环后的语句continue
结束当次循环,继续执行后续次数循环break
和continue
可以与for
和while
循环搭配使用 ```python for c in ‘Python’: if c == ‘t’:
print(c)continue
for c in ‘Python’: if c == ‘t’: break print(c)
<a name="UsN03"></a>
## 循环的高级用法
```python
for c in 'Python':
if c == 't':
continue
print(c)
else:
print('no break')
for c in 'Python':
if c == 't':
break
print(c)
else:
print('no break')
模块:random库的使用
random
库是使用随机数的Python标准库
- 伪随机数:采用梅森旋转算法生成的(伪)随机序列中元素
- random 库主要用于生成随机数
- 使用random库:
import random
random库包括两类函数,常用共8个
random() | 生成一个[0.0, 1.0)之间的随机小数 >>>random.random() 0.5714025946899135 |
---|---|
randint(a, b) | 生成一个[a, b]之间的整数 >>>random.randint(10, 100) 64 |
uniform(a, b) | 生成一个[a, b]之间的随机小数 >>>random.uniform(10, 100) 13.096321648808136 |
choice(seq) | 从序列seq中随机选择一个元素 >>>random.choice([1,2,3,4,5,6,7,8,9]) 8 |
shuffle(seq) | 将序列seq中元素随机排列,返回打乱后的序列 >>>s=[1,2,3,4,5,6,7,8,9] >>>random.shuffle(s) >>>print(s) >>>[3, 5, 8, 9, 6, 1, 2, 7, 4] |
实例:圆周率的计算
pi = 0
N = 100
for k in range(N):
pi += 1/pow(16, k)*(4/(8*k+1) - 2/(8*k+4) - 1/(8*k+5) - 1/(8*k+6))
print('the pi value: ', pi)
area1 = pi r
area2 = (2r) = 4 r
from random import random
n = 10000000
area1 = 0
for i in range(n):
x = random()
y = random()
dis = pow(x**2 + y**2, 0.5)
if (dis > 1) == False:
area1 += 1
pi = area1 / n * 4
print(pi)