【python笔记十一】python流程控制、for循环、while循环、if判断

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 Trueprint("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":breakprint(i)

#不运行
for i in "python":print(i)break

#p
for i in "python":if i == "t":breakprint(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:breakprint("hello")

​	c+=1

#无输出
c = 0
while c<6:print("hello")

​	c+=1break

#hello

continue

语句被用来跳过当前循环块中的剩余语句,然后继续进行下一轮循环

for i in "python":print(i)

#p

#y

#t

#h

#o

#n
for i in "python":continueprint(i)

#无输出
for i in "python":print(i)continueprint("hello")

#p

#y

#t

#h

#o

#n
for i in "python":if i == "t":continueprint(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+=1continueprint("123")

#hello

#hello

#hello

#hello

#hello

#hello
c = 0

while c<6:if c == 3:continueprint("c")

​	c+=1

#0

#1

#2
c = 0
while c<6:

​	c+=1if c == 3:continueprint("c")

#1

#2

#3

#4

#5

#6

pass

空语句,为了保持程序结构的完整

用于哪些语法上必须要有什么语句,但是程序什么也不做的场合

for i in range(0,10):pass
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值