Python中的控制流程主要通过以下几种方式实现:
-
顺序控制:程序按照从上到下的顺序依次执行每一条语句。
-
条件控制:使用
if
、elif
(else if的简写)和else
语句来根据条件执行不同的代码块。 -
循环控制:使用
for
和while
语句来重复执行一段代码,直到指定的条件不再满足。
顺序控制
# 顺序执行以下语句
print("第一步")
print("第二步")
print("第三步")
条件控制
if
语法一:
if 条件:
代码体
code1
code2
code3
......
#语法记忆方法
if+空格+条件+冒号
tab缩进+代码体
tab缩进+代码体
tab缩进+代码体
.......
if 条件:
代码体
code1
code2
code3
......
else:
代码体
code1
code2
code3
......
#语法记忆方法:
if+空格+条件+冒号:
tab缩进+代码体
tab缩进+代码体
tab缩进+代码体
......
else+冒号
tab缩进+代码体
tab缩进+代码体
tab缩进+代码体
......
语法二:三目运算
一行代码写 if else
满足条件的结果 if 条件 else 不满足条件的结果(全程无冒号)
a = 2
print('满足条件') if a>=3 else print('不满足条件')
语法三:多分枝
强调:if的多分枝一旦有一个条件成立就不再往下判断其他条件 必须有一个if elif 可以有无限个
#语法格式
if 条件一:
code1
code2
code3
...
elif 条件二:
code1
code2
code3
...
elif 条件三:
code1
code2
code3
...
......
else:
code1
code2
code3
...
示例:
成绩判定(可以输入五次成绩)
for i in range(5):
score = int(input('请输入分数'))
if score>=90:
print('优秀')
elif score>=80:
print('良好')
elif score>=70:
print('普通')
else:
print('很差')
下面是不对的,if语句是并列的,相当于四个独立的语句,每个语句都要判断如果我们输入一个大于等于90的分,将输出 优秀 良好 普通三个等级
score = int(input('请输入分数'))
if score>=90:
print('优秀')
if score>=80:
print('良好')
if score>=70:
print('普通')
else:
print('很差')
应改为如下
score = int(input('请输入分数'))
if score>=90:
print('优秀')
if 90>score>=80:
print('良好')
if 80>score>=70:
print('普通')
if score<70
print('很差')
if 条件加上逻辑运算符
cls = 'human'
sex = 'female'
age = 23
if cls == 'human' and sex == 'female' and age <= 23:
print('开始表白')
is_success = input('ni是否愿意>>>')
if is_success == '我愿意':
print('我爱你')
else:
print('愚人节快乐')
else:
print('姐姐好')
循环控制
1.什么是循环:循环是一个重复的过程
2.为何要循环:人可以重复去做某一件事,程序中必须有一种机制能控制计算机像人一样重复去做某一件事
while
语法
while 条件:
code1
code2
code3
只要条件满足就一直循环
break和continue的区别
break是终止循环,循环结束
continue是跳出本次循环,遇到continue不往下执行,回到开头继续下次循环
i = 0
while i<9:
print(i)
i+=1
if i==5:
break
#输出0 1 2 3 4,i=5时 break跳出循环,循环终止
i = 0
while i<9:
i+=1
if i==5:
continue
print(i)
#输出0 1 2 3 4 6 7 8 9,i=5时,continue跳出本次循环,不再执行print(i)所以5没有输出,回到开头继续下次
while+一个条件范围 不满足这个条件范围就会跳出循环 通过代码体可以结束循环
start = 0
while start<8:
print(start)
start+=1
for
for循环可以不依赖于索引取值,是一种通用的循环取值方式。for的循环次数是由被循环对象包含的值得个数决定的,while的循环次数是由条件决定的.
while循环遍历列表对比for循环遍历列表
while循环遍历列表
n = ['a0','b1','c2','d3','e4','f5']
i = 0
while i<len(n):
print(n[i])
i+=1
for循环遍历列表
n = ['a0','b1','c2','d3','e4','f5']
for i in n:
print(i)
for循环遍历字典
##默认遍历key值
z = {'a':0,'b':1,'c':2,'d':3,'e':4,'f':5}
for i in z:
print(i)#输出的是key值
#第二种遍历key值
for i in z.keys():
print(i)
#遍历value
for i in z.values():
print(i)
#遍历键值对
for i in z.items():
print(i)(元组形式输出)
range(起始索引,结束索引,步长)
range(结束索引) 相当于起始索引是零,步长是一
他是一个迭代器 顾头不顾尾 一般与for循环连用
a = range(5)
print(a)
#输出:range(0,5)
for i in range 5:
print(i)
#输出:0 1 2 3 4
print(list(a))
#输出:[0,1,2,3,4]
直接变成列表会浪费内存
print()自带一个参数end 默认是end=\n 所以打印一个东西会自动换行
print(1,end='')
print(1,end='')
print(1,end='')
#这样打印出来的三个1都在同一行上
可以实现自动换行或者空格
for循环嵌套打印9*9乘法口诀表
for i in range(10):
for j in range(1,i+1):
print('%s*%s=%s'%(i,j,i*j),end=' ')
print()
for循环去重
如果是可变类型,不可以用集合去重,可以用for循环实现去重
#for循环去重
info = [
{'name':'dahai','age':18},
{'name':'xialuo','age':17},
{'name':'xishi','age':16},
{'name':'dahai','age':18},
{'name':'dahai','age':18},
{'name':'dahai','age':18}
]
a = []
for i in info:
if i not in a:
a.append(i)
print(a)