if判断
语法一:
if 条件:
# 条件成立时执行的子代码块
代码1
代码2
代码3 示例:
sex='female'
age=18
is_beautiful=True if sex == 'female' and age > 16 and age < 20 and is_beautiful:
print('开始表白。。。') print('other code1...')
print('other code2...')
print('other code3...') 语法二:
if 条件:
# 条件成立时执行的子代码块
代码1
代码2
代码3
else:
# 条件不成立时执行的子代码块
代码1
代码2
代码3 示例:
sex='female'
age=38
is_beautiful=True if sex == 'female' and age > 16 and age < 20 and is_beautiful:
print('开始表白。。。')
else:
print('阿姨好。。。') print('other code1...')
print('other code2...')
print('other code3...') 语法三:
if 条件1:
if 条件2:
代码1
代码2
代码3 示例:
sex='female'
age=18
is_beautiful=True
is_successful=True
height=1.70 if sex == 'female' and age > 16 and age < 20 and is_beautiful \
and height > 1.60 and height < 1.80:
print('开始表白。。。')
if is_successful:
print('在一起。。。')
else:
print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊.')
else:
print('阿姨好。。。') print('other code1...')
print('other code2...')
print('other code3...') 语法四:
if 条件1:
代码1
代码2
代码3
elif 条件2:
代码1
代码2
代码3
elif 条件3:
代码1
代码2
代码3
.......
else:
代码1
代码2
代码3 示例:
如果成绩 >= 90,那么:优秀 如果成绩 >= 80且 < 90, 那么:良好 如果成绩 >= 70且 < 80, 那么:普通 其他情况:很差
''' score = input('please input your score: ') # score='100'
score = int(score) if score >= 90:
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')
while循环
语法:
while 条件:
代码1
代码2
代码3 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循环的两种方式 方式一:条件改为False,
在条件改为False时不会立即结束掉循环,而是要等到下一次循环判断条件时才会生效 tag=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') print('===>') 方式二: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 successful')
break
else:
print('username or password error') print('===>>>>>')
print('===>>>>>') while+continue:结束本次循环,直接进入下一次循环 示例一
count=1
while count < 6: #count=6
if count == 4:
count += 1
continue print(count)
count+=1 示例二:
while True:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '123':
print('login successful')
break
else:
print('username or password error')
# continue # 此处加continue无用 了解知识
while + else: while 条件:
代码1
代码2
代码3
else:
在循环结束后,并且在循环没有被break打断过的情况下,才会执行else的代码 tag=True
while tag:
print(1)
print(2)
print(3)
# tag=False
break
else:
print('else的代码') while 条件1:
while 条件2:
代码1
代码2
代码3 示范一:
while True:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '123':
print('login successful')
while True:
print("""
0 退出
1 取款
2 转账
3 查询
""")
choice=input('请输入您要执行的操作:') #choice='1'
if choice == '0':
break
elif choice == '1':
print('取款。。。')
elif choice == '2':
print('转账。。。')
elif choice == '3':
print('查询')
else:
print('输入指令错误,请重新输入')
break
else:
print('username or password error') 示范二:
tag=True
while tag:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '123':
print('login successful')
while tag:
print("""
0 退出
1 取款
2 转账
3 查询
""")
choice=input('请输入您要执行的操作:') #choice='1'
if choice == '0':
tag=False
elif choice == '1':
print('取款。。。')
elif choice == '2':
print('转账。。。')
elif choice == '3':
print('查询')
else:
print('输入指令错误,请重新输入')
else:
print('username or password error')

for循环
for循环的强大之处在于循环取值 l=['a','b','c','d','e'] i=0
while i < len(l):
print(l[i])
i+=1 for x in l: # x='b'
print(x) dic={'name':'egon','age':18,'gender':'male'}
for x in dic:
print(x,dic[x]) for + break
nums=[11,22,33,44,55]
for x in nums:
if x == 44:
break
print(x) for + continue
nums=[11,22,33,44,55]
for x in nums:
if x == 22 or x == 44:
continue
print(x) for + else
names=['egon','kevin1111_dsb','alex_dsb','mac_dsb'] for name in names:
if name == 'kevin_dsb':
break
print(name)
else:
print('======>') 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 i in range(5): # 0 1 2 3 4
print(i) for嵌套
for i in range(3):
for j in range(4):
print(i,j) for i in [0,1,2]: # i=1
for j in [0,1,2,3]: # j=1
print(i,j) '''
0 0
0 1
0 2
0 3 1 0
1 1
1 2
1 3 2 0
2 1
2 2
2 3
 

Day 04 if判断,while循环,for循环的更多相关文章

  1. Python基础之if判断,while循环,循环嵌套

    if判断 判断的定义 如果条件满足,就做一件事:条件不满足,就做另一件事: 判断语句又被称为分支语句,有判断,才有分支: if判断语句基本语法 if语句格式: if 判断的条件: 条件成立后做的事 . ...

  2. Python第四天 流程控制 if else条件判断 for循环 while循环

    Python第四天   流程控制   if else条件判断   for循环 while循环 目录 Pycharm使用技巧(转载) Python第一天  安装  shell  文件 Python第二天 ...

  3. 10_bash_变量_条件判断及运算_sed_循环

    shell编程: 编译器.解释器编程语言:机器语言.汇编语言.高级语言 静态语言:编译型语言 强类型(变量):变量在使用前,必须事先声明,甚至还需要初始化 事先转换成可执行格式 C/C++.C#.Ja ...

  4. JavaScript基础知识(三个判断、三个循环)

    三个判断 if…else…只会执行其中一个条件 如果if条件中只有一个值,那么会默认转布尔: if(1=="1"){ // 当括号中条件为true时,执行此处的代码 console ...

  5. python-if条件判断与while/for循环

    条件判断if 让计算机像人一样,能判断是非对错,根据条件做一些事情. if ''' ------ if代码结构:------- if 条件: 代码体 tips:同一缩进范围内的代码被视作同一代码体,p ...

  6. Python中的条件判断、循环以及循环的终止

    条件判断 条件语句是用来判断给定条件是否满足,并根据判断所得结果从而决定所要执行的操作,通常的逻辑思路如下图: 单次判断 形式 if <判断条件>: <执行> else: &l ...

  7. 判断语句 、 while循环 、 for循环

    判断语句 语法结构 if 条件1: 如果条件1为真,执行语句块 elif 条件2: 如果条件2为真,执行语句块 elif 条件3: 如果条件2为真,执行语句块 elif 条件n: 如果条件n为真,执行 ...

  8. js循环(while循环,do while循环,for循环)相关知识点及练习

    08.循环 1.循环! 循环的作用: 简化代码,处理重复执行的代码 遍历数组.json对象.节点集合 2.while循环 语法: while(循环的条件){ 循环体 } 3.循环的五大要素 循环变量 ...

  9. java 流程执行 循环 foreach循环

    一. if分支 1. 结构  if  else if   else 2.执行原则 if  if  if 结构  会一直去执行()里的判断语句 if else if  else if 结构  只要一条( ...

随机推荐

  1. 漫步Java------接口

    接口 一.定义 具有相同行为(方法),但是不相关的类 二.特点 只是提供方法,不定义方法的具体实现. 一个类只能继承一个父类,但是接口却可以继承多个接口. 接口是一个引用类型的变量 接口没有构造方法, ...

  2. 百杂讲堂之为什么32位系统只能操作4g内存

    百杂讲堂之为什么32位系统只能操作4g内存 计算机内存中很多的单元,每一个单元就是一个字节,一个字节有8位.每一个单元有两种状态:0和1. 所以 两个单元就有4个组合: 3个单元就有8个组合: 依次类 ...

  3. excel idea sql 操作

    1.excel  concatenate()函数中连接单元格中值拼接sql时,内容超长,把insert into.... values()前面的单独提出来,只在concatenate()中拼接valu ...

  4. Ubuntu 16.04安装vsftpd 并开启ftp服务

    1. 安装 sudo apt-get install vsftpd 2.可以使用下列命令来打开,关闭,重启ftp服务 sudo /etc/init.d/vsftpd start sudo /etc/i ...

  5. 2.34 jquery定位

    2.34 jquery定位(简直逆天) 前言元素定位可以说是学自动化的小伙伴遇到的一道门槛,学会了定位也就打通了任督二脉,前面分享过selenium的18般武艺,再加上五种js的定位大法.这些还不够的 ...

  6. 正则表达式 —— Cases 与 Tricks

    1. cases 匹配任意单词(两侧可以有多个空格): ( +[a-zA-Z]+ +) 上述表达式无法匹配句子末尾的单词,若想匹配句尾或者逗号前的单词,则可拓展为: ( +[a-zA-Z]+[?,.] ...

  7. 【转载】 深度学习总结:用pytorch做dropout和Batch Normalization时需要注意的地方,用tensorflow做dropout和BN时需要注意的地方,

    原文地址: https://blog.csdn.net/weixin_40759186/article/details/87547795 ------------------------------- ...

  8. 爬格子问题(经典强化学习问题) Sarsa 与 Q-Learning 的区别

    SARSA v.s. Q-learning 爬格子问题,是典型的经典强化学习问题. 动作是上下左右的走,每走一步就会有一个-1的奖赏.从初始状态走到最终的状态,要走最短的路才能使奖赏最大.图中有一个悬 ...

  9. ubuntu下配置环境

    https://blog.csdn.net/zzc15806/article/details/73662491

  10. 【BZOJ1930】【SHOI2003】吃豆豆

    初见杀…… 原题: 两个PACMAN吃豆豆.一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方.PACMAN走到豆豆处就会吃掉它.PACMAN行走的路线很奇怪,只能向右走或者向上走,他们行 ...