笔记十一
python流程控制
python的条件语句
if…elif…else…分支语句
条件为真才执行if里面的语句
条件为假不执行if里面的语句
if 3>2:
print('你好')
#你好
elif 4>1:
print('hello')
#不执行
#当满足第一个条件时,后面的elif即使成立也不运行了
if 1>2:
print('你好')
elif 4>1:
print('hello')
elif 4>2:
print('123')
#hello
if 1>2:
print("hello")
else:
print("以上条件均不满足")
#以上条件均不满足
示例:
a = int(input('请输入你的成绩'))
if a>=85:
print("优秀")
elif a>=75:
print("良好")
elif a>=60:
print("及格")
else:
print("不及格")
#示例:输入99 输出优秀;输入59 输出不及格
for循环
for循环用来遍历序列
通过不使用下标的方式来实现对序列中每一个元素的访问
遍历列表、元组、字符串、集合
for i in [1,2,3]:
print(i)
#1
#2
#3
for i in "1,2,3":
print(i)
#1
#2
#3
for i in (1,2,3):
print(i)
#1
#2
#3
for i in {1,2,3}:
print(i)
#1
#2
#3
遍历字典
for i in {"名字":"KR","年龄":18}:
print(i)
#名字
#年龄
d = {
"名字":"KR",
"年龄":18
}
for a in d:
print(a)
#名字
#年龄
遍历字典键跟值
for i in d.items():
print(i)
#("名字":"KR")
#("年龄":18)
遍历数字range()
#取左不取右
for i in range(0,10):
print(i)
#0
#1
#2
#3
#4
#5
#6
#7
#8
#9
for i in range(0,10,2):
print(i)
#0
#2
#4
#6
#8
双层for循环
a = [1,2,3,[10,20,30],[100,200,300]]
for i in a:
print(a)
#1
#2
#3
#[10,20,30]
#[100,200,300]
if判断& isinstance()
isinstance()用来判断一个对象是否是一个已知的类型
for i in a:
printa(i)
if isinstance(i,list)
for x in i:
print(x)
#10
#20
#30
#100
#200
#300
while循环
while True
print("hello")
#会一直输出hello 不会停止
c = 0
while c<6:
print("hello")
c+=1
else:
ptint("循环终止")
#0
#1
#2
#3
#4
#5
#循环终止
循环中的关键字
break
语句可以跳出for和while的循环体
for i in "python":
print(i)
#p
#y
#t
#h
#o
#n
for i in "python":
break
print(i)
#不运行
for i in "python":
print(i)
break
#p
for i in "python":
if i == "t":
break
print(i)
#p
#y
for i in "python":
print(i)
if i == "t":
break
#p
#y
#t
c = 0
while c<6:
print("hello")
c+=1
#hello
#hello
#hello
#hello
#hello
#hello
c = 0
while c<6:
break
print("hello")
c+=1
#无输出
c = 0
while c<6:
print("hello")
c+=1
break
#hello
continue
语句被用来跳过当前循环块中的剩余语句,然后继续进行下一轮循环
for i in "python":
print(i)
#p
#y
#t
#h
#o
#n
for i in "python":
continue
print(i)
#无输出
for i in "python":
print(i)
continue
print("hello")
#p
#y
#t
#h
#o
#n
for i in "python":
if i == "t":
continue
print(i)
#p
#y
#h
#o
#n
for i in "python":
print(i)
if i == "t":
continue
#p
#y
#t
#h
#o
#n
c = 0
while c<6:
print("hello")
c+=1
continue
print("123")
#hello
#hello
#hello
#hello
#hello
#hello
c = 0
while c<6:
if c == 3:
continue
print("c")
c+=1
#0
#1
#2
c = 0
while c<6:
c+=1
if c == 3:
continue
print("c")
#1
#2
#3
#4
#5
#6
pass
空语句,为了保持程序结构的完整
用于哪些语法上必须要有什么语句,但是程序什么也不做的场合
for i in range(0,10):
pass