一、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 == '':
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 == '':
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 == '':
print('login successful')
break
else:
print('username or password error') print('===>>>>>')
print('===>>>>>')
2.1、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 == '':
print('login successful')
break
else:
print('username or password error')
# continue # 此处加continue无用
2.2、while else
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的代码')

2.3、while嵌套

 #语法
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 == '':
print('login successful')
while True:
print("""
0 退出
1 取款
2 转账
3 查询
""")
choice=input('请输入您要执行的操作:') #choice='1'
if choice == '':
break
elif choice == '':
print('取款。。。')
elif choice == '':
print('转账。。。')
elif choice == '':
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 == '':
print('login successful')
while tag:
print("""
0 退出
1 取款
2 转账
3 查询
""")
choice=input('请输入您要执行的操作:') #choice='1'
if choice == '':
tag=False
elif choice == '':
print('取款。。。')
elif choice == '':
print('转账。。。')
elif choice == '':
print('查询')
else:
print('输入指令错误,请重新输入')
else:
print('username or password error')

示例二

三、for循环

1 迭代式循环:for,语法如下

  for i in range(10):

    缩进的代码块

2 break与continue(同上)

3 循环嵌套

 for i in range(1,10):
for j in range(1,i+1):
print('%s*%s=%s' %(i,j,i*j),end=' ')
print()

九九乘法表

 

python 条件分支与循环的更多相关文章

  1. python3.4学习笔记(十) 常用操作符,条件分支和循环实例

    python3.4学习笔记(十) 常用操作符,条件分支和循环实例 #Pyhon常用操作符 c = d = 10 d /= 8 #3.x真正的除法 print(d) #1.25 c //= 8 #用两个 ...

  2. Python - 条件控制、循环语句 - 第十二天

    Python 条件控制.循环语句 end 关键字 关键字end可以用于将结果输出到同一行,或者在输出的末尾添加不同的字符,实例如下: Python 条件语句是通过一条或多条语句的执行结果(True 或 ...

  3. Python——条件语句及其循环

    条件语句及其循环 一. 条件语句 在条件语句中可以使用以下所有的运算符: 算术运算符:+.-.*././/.%.** 关系运算符:>.<.==.<=.>=.!= 测试运算符:i ...

  4. C#(三)基础篇—方法,递归,条件分支,循环,三元操作符

    C# 本随笔为个人复习巩固知识用,多从书上总结与理解得来,如有错误麻烦指正 2020-12-03 1.方法 static void Main(string[] args) { float Sum(fl ...

  5. Python(四) 分支、循环、条件与枚举

    一.什么是表达式 表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列 二.表达式的优先级 三.表达式优先级练习 优先级同级 从左往右计算 1 or 2 a ...

  6. 初学python(print使用、条件分支、循环、模块引用)

    import random """ #查看源代码日后爬虫用 import urllib.request # coding=utf-8 url = "http:/ ...

  7. python条件判断与循环

    条件判断 1.python缩进规则: 如果if语句判断是True,就把缩进的语句执行了,否则,什么也不做,比如: age=20 if age >= 18: print('your age is' ...

  8. Python条件控制与循环语句

    1. 条件控制 # if-elif-else结构 age = 12 if age < 4: price = 0 elif age < 18: price = 5 else: price = ...

  9. Python条件判断和循环,range()函数

    条件判断经常使用if语句进行判断,表达方式为:if 条件语句:      :elif:else if...用于执行第一条不满足if的判断,继续执行其它的判断.比如一个简单的if判断 Python3取消 ...

随机推荐

  1. 通过hash实现前端路由

    router.js //构造函数 function Router() { this.routes = {}; this.currentUrl = ''; } Router.prototype.rout ...

  2. HTML和CSS在IE7中常见的兼容性问题

    IE7及以下版本都会有这些问题 1.IE7块转内联块问题 问题描述:好像块转内联块失败,依然不在一行排列 解决办法:给元素添加如下css    *display:inline;*zoom:1; *di ...

  3. HTML5移动端拖动惯性

    下面代码只是实现了上下滑动惯性,没有写水平滑动惯性.(临时代码笔记,可能会在以后的过程中不断更新优化代码) /** * 惯性原理: * 产生的速度 = 移动距离 / 移动时间 * 距离 = 松开的坐标 ...

  4. 35.Odoo产品分析 (四) – 工具板块(6) – 午餐管理(1)

    查看Odoo产品分析系列--目录 很多公司为都会为员工提供午餐.然而,公司内部的午餐需要适当的管理,特别是在员工或供应商数量非常重要的时候."午餐订单"模块的开发,使管理更容易,也 ...

  5. 第三篇 Html-label标签

    label标签 用户获取文字,使得关联的标签获取光标 <!DOCTYPE html> <html lang="en"> <head> <m ...

  6. 01-vue学习之前的准备

    一.具备的基础知识 1.扎实的HTML/CSS/Javascript基本功,这是前置条件. 2.不要用任何的构建项目工具,只用最简单的<script>,把教程里的例子模仿一遍,理解用法.不 ...

  7. asp.net --> 初识WCF

    对于刚开始接触wcf的同志,可以有效的理解wcf的使用场景. 引用该文章(点击查看),简单的介绍wcf的使用.另一篇文章(点击查看),和上述文章内容差不多.

  8. SQL server 远程连接不成功解决

    一直以来打算自己做一个博客网站,前段时间开始准备做了,正好碰上新睿云服务器免费一年的活动,赶紧拿下.装好了sqlserver ,用本地访问没有问题,但是关键是外网访问一直不行找了好多资料最终才搞定.下 ...

  9. Swift4.0 从相册中获取图片和拍照

    第一步 添加协议 UIImagePickerControllerDelegate,UINavigationControllerDelegate   第二步 添加选择方式 let sexActionSh ...

  10. Linux 自动化部署DNS服务器

    Linux 自动化部署DNS服务器 1.首先配置主DNS服务器的IP地址,DNS地址一个写主dns的IP地址,一个写从dns的地址,这里也可以不写,在测试的时候在/etc/resolv.conf中添加 ...