条件判断 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('===>>>>>')
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无用
了解知识 
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 == '':
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循环

  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

break打断,则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嵌套
for i in range(3):
for j in range(4):
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

04 if条件判断 流程控制的更多相关文章

  1. 04 Python入门学习-流程控制(if else elif while for)

    一:流程控制if 语法一: if 条件: code1 code2 code3 ... age = 20 height = 170 weight = 60 sex = 'female' is_beaut ...

  2. 从头开始-04.C语言中流程控制

    分支结构: if语句:当条表达式满足的时候就执行if后面大括号中语句 三种格式: if,if else , if else if else 特点:1.只有一个代码块会被执行 2.若有else那么必有一 ...

  3. [基本运算符、流程控制之if判断、与用户交互、深浅拷贝]

    [基本运算符.流程控制之if判断.与用户交互] 基本运算符 1.算数运算符 python支持的算术运算符与数学上计算的符号使用是一致的 salary = 3.3 res = salary * 12 p ...

  4. 数据类型(三) + 流程控制(一) day05

    目录 昨日回顾 (三) 花式赋值 链式赋值 交叉赋值 (四) 列表list (五) 字典dict (六) 布尔值 (七) 解压缩 (八) python与用户交互的方式 (九) 三种格式化输出的方式 f ...

  5. 基本运算符,流程控制if、while

    基本运算符 算术运算符 运算符 描述 实例 + 加 a + b - 减 a - b * 乘 a * b / 除 a / b % 取余 a % b // 整除 a // b ** 幂运算 a ** b ...

  6. [Shell]条件判断与流程控制:if, case, for, while, until

    ---------------------------------------------------------------------------------------------------- ...

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

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

  8. shell编程-条件判断与流程控制

    1.条件判断式 按照文件类型进行判断: 两种判断格式: test -e /root/install.log [ -e /root/install.log ] 判断命令是否正确执行: [ -d /roo ...

  9. Shell脚本 (三) 条件判断 与 流程控制

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 六.条件判断 1.基本语法 [ condition ](注意condition 前后要有空格) 注意:条 ...

随机推荐

  1. Android MediaPlayer播放raw资源封装类

    import android.content.Context; import android.media.MediaPlayer; import xxxx.R; public class MediaU ...

  2. 三十、Linux 进程与信号——信号的概念及 signal 函数

    30.1 信号的基本概念 信号(signal)机制是Linux 系统中最为古老的进程之间的通信机制,解决进程在正常运行过程中被中断的问题,导致进程的处理流程会发生变化 信号是软件中断 信号是异步事件 ...

  3. luogu 3045 优先队列反悔/bzoj 2590

    N头奶牛,价格Pi,K张优惠券,优惠券购买降为Ci,不超过M的钱最多可买多少奶牛 先将c值k小的加入,将它们省下的钱加入优先队列(省下的钱由少到多),在将k+1-n用p排序,再逐个与优先队列中弹出的比 ...

  4. 20155324 《Java程序设计》实验四 Android开发基础

    20155324 <Java程序设计>实验四 Android开发基础 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件. ...

  5. ASP.NET 配置log4net启用写错误日志功能

    http://www.cnblogs.com/yeminglong/archive/2013/05/21/3091192.html 首先我们到apche的官网下载log4net的项目编译得到log4n ...

  6. Java SE之正则表达式三:替换

    /** * * @author Zen Johnny * @date 2018年4月29日 下午4:31:07 * */ package demo.regex; public class RegexR ...

  7. Coursera, Deep Learning 5, Sequence Models, week2, Natural Language Processing & Word Embeddings

    Word embeding 给word 加feature,用来区分word 之间的不同,或者识别word之间的相似性. 用于学习 Embeding matrix E 的数据集非常大,比如 1B - 1 ...

  8. PHP面试(一):PHP基础知识考察点、网页考察点、Linux考察点、MySQL考察点

    一.基础知识考察 1.引用变量的概念及定义方式——引用变量的原理 2.常量及数据类型——字符串的三种定义方法及各自的区别 3.运算符的使用——错误控制符.运算符优先级 4.流程控制操作 5.自定义函数 ...

  9. win7, ubuntu双系统,重装win7后,修复引导

    参考: http://blog.csdn.net/abcsunl/article/details/72875983 http://blog.csdn.net/davied9/article/detai ...

  10. Python 获取文件中最长行的长度和最长行

    1, 使用文件 #vim /etc/motd "1 hello world" 2 ...... yes 3 no you are a shadiao 4 hahh maye you ...