第四章python的程序控制结构
程序的基本结构:
- 顺序结构
- 分支结构
- 循环结构
分支结构中常用的6种关系操作符
条件组合,在Python是用and和or还有not组合
基本结构
if a>b:
print()
elif a<b:
print()
else:
print()
循环结构
基本结构:
for <循环变量> in <遍历结构>:
语句块
例:求1-100的和
for i in range(100):
a=a+i;
print(a)
无线循环
while true:
语句块
循环保留字break和continue
break是退出当前循环,如果外层循环还在,继续执行外层循环。
continue本次循环不执行继续本层循环。
for s in 'bit':
for i in range(4):
print(s,end="")
if s=='i':
break
random库的使用
from random import *
a=0.0
print(int(random()*100))
print(randint(1,10))