fushuanglin

723 if while for

if == 如果
程序结构分为三种
顺序结构 程序按照从上往下的顺序依次执行
分支结构 程序根据某种条件选择要执行的代码
循环结构 可以使代码重复的结构

需求如果温度高于30就开空调


while
for

 

有序与无序
进制转换

 

1、if判断
if 条件1:
pass
elif 条件2:
pass
elif 条件3:
pass
else:
pass


count=0
if 条件1:
print('xxxx')
count+=1
else:
print('yyyy')
count+=1

优化:
count=0
if 条件1:
print('xxxx')
else:
print('yyyy')
count+=1

2、while条件循环
while 条件:
code1
code2
code3
code4

while + break:break结束本层循环
while + continue:continue结束本次循环(跳过了本次循环之后的代码),直接进入下一次
while的条件:tag=True
while+else:else的子代码块会在while循环没有被break打断的情况下最后执行


3、for循环
通常用于取值(不依赖索引)
for item in 包含多个值的数据类型:
pass

for i in range(0,5): #0 1 2 3 4
print(i)

for i in range(0,5,2): #0 2 4
print(i)

for+break
for+continue
for+else


4、有序or无序
有序:能通过索引取值的数据类型都是有序的

5、可变or不可变
x=10
x=11
可变:在值变的情况下,id不变,证明就是在改原值
可变:在值变的情况下,id也跟着变,证明是造出一个新值,并没有改原值

posted on 2018-07-24 08:28  fushuanglin  阅读(130)  评论(0编辑  收藏  举报

导航