DAY04、流程控制if、while、for
一、if 判断
语法一:
if 条件:
# 以下是上一条if 的子代码块
print(子代码1)
print(子代码2)
print(子代码3) 示例:
# 路边飘过一个生物,要不要表白?
sex = 'female'
age = 18
is beautiful = True if sex == 'female' and age == 18 and is beautiful:
print('我要表白') # 子代码块代码
print('上一缩进级的代码') # 上一缩进级的代码块
print('上一缩进级的代码')
print('上一缩进级的代码') if 运行完之后会执行代码下方 上一缩进级 的代码块 语法二:
if 条件:
# 以下缩进级是条件成立时执行的代码块
print(子代码1)
print(子代码2)
print(子代码3)
else:
# 以下缩进级是条件不成立时执行的代码块
print(子代码4)
print(子代码5)
print(子代码6) 示例:
# 路边飘过一个生物,要不要表白?
sex = 'female'
age = 18
is beautiful = True if sex == 'female' and age == 18 and is beautiful:
print('开始表白')
else:
print('搁置表白') 语法三:
if 条件1:
print(子代码1)
print(子代码2)
print(子代码3)
elif 条件2:
print(子代码4)
print(子代码5)
print(子代码6)
elif 条件3:
print(子代码7)
print(子代码8)
print(子代码9)
else:
print(子代码10)
print(子代码11)
print(子代码12) 示例:
'''
# 需求描述:
如果成绩 >= 90,那么:优秀
如果成绩 >= 80且 < 90, 那么:良好
如果成绩 >= 70且 < 80, 那么:普通
其他情况:很差
''' score = input('please input your score:')
if score >= 90:
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差') 语法四:(if判断的嵌套)
sex = 'female'
age = 18
is beautiful = True
is cuccessful = True if sex == 'female' and age == 18 and is beautiful:
print('开始表白')
if is cuccessful:
print('在一起')
else:
print('有缘无分')
else:
print('阿姨好') 二、while循环
语法:
while 条件:
print('代码1')
print('代码2')
print('代码3') 示例:
# 循环输入账号密码,并嵌套if判断来打印相输入账号密码后的结果
while True:
name = input('please input your name:')
pwd = input('please input your password')
if name == 'egon' and pwd == 123
print('login successful')
else:
print('username or password error') 结束while循环的两种方式:
方式一:将while条件改为False
在条件改为False时不会立即结束掉循环,而是要等到下一次循环判断条件时才会生效 示例:
# 在输入正确账号密码之后就停止再次输入账号
tag = True # 将True赋值给一个变量,让变量的真假成为条件,通过更改变量的真假来达到改变条件的真假
while tag:
name = input('please input your name')
pwd = input('please input your password')
if name == 'egon' and pwd == '123':
print('login successful')
tag = False
else:
print('username or password error') 方式二:while + break
break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环 示例:
# 在输入正确账号密码之后就停止再次输入账号
while True:
name = input('please input your name')
pwd = input('please input your password')
if name == 'egon' and pwd == '123':
print('login success')
break
else:
print('username or password error') 中止while的方式:(不是终止)
while + continue:结束本次循环,直接进入下一次循环 示例:
# 除了4,取1-6
count = 1
while count < 7:
if count == 4:
count += 1
'''
因为本次循环到此结束,想要让代码继续运行下去,就必须要对count今天增量赋值
continue下面的增量赋值指令是无法被执行的,但是增量赋值还是要进行
所以加在contine上面
'''
continue
print(count)
count += 1 注意:continue加在循环的最后一行便是无用代码,因为循环最后一行运行完之后自动会重新进行循环,无需continue来中止循环 了解知识:
while + else while 条件:
代码1
代码2
代码3
else:
在循环结束后,并且在循环没有被break打断过的情况下,才会执行else的代码 示例:
tag=True
while tag:
print(1)
print(2)
print(3)
tag=False
'''
此处利用将条件改为False是循环结束,但是还是会最后运行完接下来的代码的
运行到break,else的代码还是不会被执行
'''
break
else:
print('else的代码') while循环的嵌套: while 条件1:
while 条件2:
代码1
代码2
代码3 示例一:
'''
在输错就循环,输对就结束程序的基础上加上:
输对之后进入下一层功能界面,其中的退出功能可直接终止循环
'''
while True:
name = input('please input your name')
pwd = input('please inpunt your password')
if name == 'egon' and pwd == '123':
print('login successful')
while True:
print('''
0 退出
1 取钱
2 转账
3 查询
''')
choice = input('请选择您要执行的操作:')
if choice == '0':
break # 终止此层循环,执行上一缩进级的吓一条代码,即 #1 break
elif choice == '1':
print('取钱')
elif choice == '2':
print('转账')
elif choice == '3':
print('查询')
else:
print('操作有误,请重新选择:')
break # 此处为 #1 break
else:
print('用户名或者密码错误!') 示例二:
# 两个while循环使用相同的可变量True ,只要运行到 '0 退出'就直接封锁所有whjle循环
tag = True
while tag:
name = input('please input your name:')
pwd = input('please input your password:')
if name == 'egon' and pwd == '123':
print('login successgul')
while tag:
print('''
0 退出
1 取款
2 转账
3 查询
''')
choice = input('请选择您要执行的操作:')
if choice == '0':
tag = False
elif choice == '1':
print('取款')
elif choice == '2':
print('转账')
elif choice == '3':
print('查询')
else:
print('操作有误,请重新选择:')
else:
print('用户名或者密码错误!') 三、for循环:
for循环的强大之处在于循环取值 示例:
# 取出列表中的值并打印
list = [a,b,c,d,e]
# 使用while循环取值:
i = 0
while i < 6:
print(list[i]) # i作为整型变量充当列表中的索引存在于 list[i]中,从而达到 顺延索引值 来取值的效果
i += 1
# 使用for循环取值:
for i in list:
'''
# 这层for循环第一次循环时,这一行其实就是 i = list[1],把列表中的第一个值赋值给i
'''
print(i) # 取出字典中的值并打印
dic={'name':'egon','age':18,'gender':'male'}
for i in dic:
print(i,dic[i])
# 这里的i为字典中的key,故dic[i]则为字典中key_i对应的value for + continue:中止本次for循环(不是终止) 示例:
# 打印到xx就跳过此次打印
nums=[11,22,33,44,55]
for i in nums:
if i == 44
continue
print(i) for + break:终止本层for循环 示例:
# 打印到xx就停止打印
nums=[11,22,33,44,55]
for i in nums:
if i == 44:
break
print(i) for + else : 示例:
names=['egon','kevin1111_dsb','alex_dsb','mac_dsb']
for name in names:
if name == 'kevin1111_dsb'
break # 一旦因break而终止循环的情况,上一个缩进级的else代码便不会被执行
print(name)
else:
print('else测试代码') for + range():
range的用法
>>> range(1,5)
[1, 2, 3, 4]
>>> for i in range(1,5):
... print(i)
...
1
2
3
4
>>> range(1,5,1)
[1, 2, 3, 4]
>>> range(1,5,2) # 1 3
[1, 3] for循环的嵌套: 示例:
for i in range(3):
for j in range(4):
print(i,j)
# 无法理解的就看下面的代码,即将range转换为列表方便理解
for i in [0,1,2]: # i=1
for j in [0,1,2,3]: # j=1
'''
# 执行完下层缩进级的for循环,
才会回到上层缩进级的for循环去继续循环未完成的上层循环
'''
print(i,j)
DAY04、流程控制if、while、for的更多相关文章
- day04 流程控制
在python中流程控制主要有三种:顺序流程.分支流程.循环流程 1.顺序流程:在宏观上,python程序的运行就是自上而下的顺序流程: 2.分支流程:分支流程主要是 if...else....流程 ...
- day04流程控制,if分支结构,while,for循环
复习 ''' 1.变量名命名规范 -- 1.只能由数字.字母 及 _ 组成 -- 2.不能以数字开头 -- 3.不能与系统关键字重名 -- 4._开头有特殊含义 -- 5.__开头__结尾的变量,魔法 ...
- day04流程控制之while循环
流程控制之while循环 1.什么是while循环 循环指的是一个重复做某件事的过程 2.为何有循环 为了让计算机能像人一样重复 做某件事 3.如何用循环 ''' # while循环的语法:while ...
- Day04 流程控制 while 和for循环
一.流程控制 if 判断 python中使用缩进来区分代码块的 语法 一: #python if 条件: 代码块1 代码块2 自上而下依次运行 语法二: # python if 条件一: 代码一 el ...
- day04流程控制之while
while语法: while 条件: 缩进的循环体 # 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件. # 如果条件为假,那么循环体不执行,循环终止 示例一: "&qu ...
- day04 运算符 流程控制 (if while/of)
1. 运算符算数运算符 + - * / int / float :数字类型 # print(10 + 3.1)# print(10 / 3)# print(10 // 3)# print(10 % 3 ...
- 第10章 Shell编程(4)_流程控制
5. 流程控制 5.1 if语句 (1)格式: 格式1 格式2 多分支if if [ 条件判断式 ];then #程序 else #程序 fi if [ 条件判断式 ] then #程序 else # ...
- Shell命令和流程控制
Shell命令和流程控制 在shell脚本中可以使用三类命令: 1)Unix 命令: 虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令.这些命令通常是用来进行文件和文字 ...
- PHP基础知识之流程控制的替代语法
PHP 提供了一些流程控制的替代语法,包括 if,while,for,foreach 和 switch. 替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,e ...
- Python黑帽编程2.4 流程控制
Python黑帽编程2.4 流程控制 本节要介绍的是Python编程中和流程控制有关的关键字和相关内容. 2.4.1 if …..else 先上一段代码: #!/usr/bin/python # - ...
随机推荐
- 01 python初学(注释、交互、if while for)
为了能生存下去,一定要坚持学习! 目录 1. 注释 2. 用户交互 3. if .while.for 语句 1. 注释 单行注释: # 多行注释: 三个单引号 || 三个双引号 2. 用户交互: ...
- ORA-19566: exceeded limit of 0 corrupt blocks for file E:\xxxx\<datafilename>.ORA.
How to Format Corrupted Block Not Part of Any Segment (Doc ID 336133.1) To BottomTo Bottom In this D ...
- PyCharm 中使用 Pylint 控制代码质量
1) Pylint安装 - Windows下: 直接在 cmd 下使用 pip install pylint 即可(如果 pip 不可用,首先安装最新版 Python,会默认安装 pip,或者找到 p ...
- mysql概述
MySql大致分为三层结构: 第一层:客户端并非MySql所独有,例如:连接处理.授权认证.安全等功能均在这一层处理 第二层:核心服务包括查询解析.分析.优化.缓存.内置函数(比如 : 时间.数学.加 ...
- 启动项目显示:非法字符:'\ufeff' 和需要 class ,interface 或者 enum 错误
原来是因为 Windows 记事本在修改 UTF-8 文件时自作聪明地在文件开头添加 BOM 导致的,所以才会导致 IDEA 不能正确读取 .java 文件从而程序出错. 解决: 找到 xxx. ja ...
- linux驱动之中断处理过程C程序部分
当发生中断之后,linux系统在汇编阶段经过一系列跳转,最终跳转到asm_do_IRQ()函数,开始C程序阶段的处理.在汇编阶段,程序已经计算出发生中断的中断号irq,这个关键参数最终传递给asm_d ...
- dubbo源码阅读
http://seekheap.com/posts/dubbo/dubbo-src-01-overview-and-debug-environment.html 先占坑
- 算法相关——Java排序算法之希尔排序(五)
个子块,即{3,5},{1,0},{5,2},{9,4},{6,12},将每个子块进行插入排序(即第i位与第i+5位进行比较交换),初步排序结果为{3,0,2,4,6,5,1,5,9,12}.希尔排序 ...
- Java多线程学习(二)---线程创建方式
线程创建方式 摘要: 1. 通过继承Thread类来创建并启动多线程的方式 2. 通过实现Runnable接口来创建并启动线程的方式 3. 通过实现Callable接口来创建并启动线程的方式 4. 总 ...
- Python_每日习题_0007_copy
题目:将一个列表的数据复制到另一个列表中. 程序分析:使用列表[:],拿不准可以调用copy模块 import copy a = [,,,,['a','b']] b = a #赋值 c = a[:] ...