条件判断 if

  1. 语法一:
    if 条件: # 条件成立时执行的子代码块
    代码1
    代码2
    代码3
  1. 示例:
  2. sex='female'
  3. age=18
  4. is_beautiful=True
  5.  
  6. if sex == 'female' and age > 16 and age < 20 and is_beautiful:
  7. print('开始表白。。。')
  8.  
  9. print('other code1...')
  10. print('other code2...')
  11. print('other code3...')
  1. 语法二:
    if 条件: # 条件成立时执行的子代码块
    代码1
    代码2
    代码3
    else: # 条件不成立时执行的子代码块
      代码1
      代码2
      代码3
  1. 示例:
  2. sex='female'
  3. age=38
  4. is_beautiful=True
  5.  
  6. if sex == 'female' and age > 16 and age < 20 and is_beautiful:
  7. print('开始表白。。。')
  8. else:
  9. print('阿姨好。。。')
  10.  
  11. print('other code1...')
  12. print('other code2...')
  13. print('other code3...')
  1. 语法三:
    if 条件1:
      if 条件2:
        代码1
        代码2
        代码3
  1. 示例:
  2. sex='female'
  3. age=18
  4. is_beautiful=True
  5. is_successful=True
  6. height=1.70
  7.  
  8. if sex == 'female' and age > 16 and age < 20 and is_beautiful \
  9. and height > 1.60 and height < 1.80:
  10. print('开始表白。。。')
  11. if is_successful:
  12. print('在一起。。。')
  13. else:
  14. print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊.')
  15. else:
  16. print('阿姨好。。。')
  17.  
  18. print('other code1...')
  19. print('other code2...')
  20. print('other code3...')
  1. 语法四:
    if 条件1:
      代码1
      代码2
      代码3
    elif 条件2:
      代码1
      代码2
      代码3
    elif 条件3:
      代码1
      代码2
      代码3
    .......
    else:
      代码1
      代码2
      代码3
  1. 示例:
  2.  
  3. 如果成绩 >= 90,那么:优秀
  4.  
  5. 如果成绩 >= 80 < 90, 那么:良好
  6.  
  7. 如果成绩 >= 70 < 80, 那么:普通
  8.  
  9. 其他情况:很差
  10.  
  11. score = input('please input your score: ') # score='100'
  12. score = int(score)
  13.  
  14. if score >= 90:
  15. print('优秀')
  16. elif score >= 80:
  17. print('良好')
  18. elif score >= 70:
  19. print('普通')
  20. else:
  21. print('很差')

流程控制:while循环

  1. 语法:
    while 条件:
      代码1
      代码2
      代码3
  1. while True:
  2. name=input('please input your name: ')
  3. pwd=input('please input your password: ')
  4.  
  5. if name == 'egon' and pwd == '':
  6. print('login successful')
  7. else:
  8. print('username or password error')
  1. 结束while循环的两种方式
    方式一:条件改为False
      在条件改为False时不会立即结束掉循环,而是要等到下一次循环判断条件时才会生效
  1. tag=True
  2. while tag:
  3. name=input('please input your name: ')
  4. pwd=input('please input your password: ')
  5.  
  6. if name == 'egon' and pwd == '':
  7. print('login successful')
  8. tag=False
  9. else:
  10. print('username or password error')
  11.  
  12. print('===>')
  1. 方式二:while+break
      break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环
  1. while True:
  2. name=input('please input your name: ')
  3. pwd=input('please input your password: ')
  4.  
  5. if name == 'egon' and pwd == '':
  6. print('login successful')
  7. break
  8. else:
  9. print('username or password error')
  10.  
  11. print('===>>>>>')
  12. print('===>>>>>')
  1. while+continue:结束本次循环,直接进入下一次循环
  1. 示例一
  2. count=1
  3. while count < 6: #count=6
  4. if count == 4:
  5. count += 1
  6. continue
  7.  
  8. print(count)
  9. count+=1
  1. 示例二:
  2. while True:
  3. name=input('please input your name: ')
  4. pwd=input('please input your password: ')
  5.  
  6. if name == 'egon' and pwd == '':
  7. print('login successful')
  8. break
  9. else:
  10. print('username or password error')
  11. # continue # 此处加continue无用
  1. 了解知识
    while + else:
    while 条件:
      代码1
      代码2
      代码3
    else:
      在循环结束后,并且在循环没有被break打断过的情况下,才会执行else的代码
  1. tag=True
  2. while tag:
  3. print(1)
  4. print(2)
  5. print(3)
  6. # tag=False
  7. break
  8. else:
  9. print('else的代码')

while 条件1:

while 条件2:    
  代码1    
  代码2    
  代码3

  1. 示范一:
  2. while True:
  3. name=input('please input your name: ')
  4. pwd=input('please input your password: ')
  5.  
  6. if name == 'egon' and pwd == '':
  7. print('login successful')
  8. while True:
  9. print("""
  10. 0 退出
  11. 1 取款
  12. 2 转账
  13. 3 查询
  14. """)
  15. choice=input('请输入您要执行的操作:') #choice='1'
  16. if choice == '':
  17. break
  18. elif choice == '':
  19. print('取款。。。')
  20. elif choice == '':
  21. print('转账。。。')
  22. elif choice == '':
  23. print('查询')
  24. else:
  25. print('输入指令错误,请重新输入')
  26. break
  27. else:
  28. print('username or password error')
  1. 示范二:
  2. tag=True
  3. while tag:
  4. name=input('please input your name: ')
  5. pwd=input('please input your password: ')
  6.  
  7. if name == 'egon' and pwd == '':
  8. print('login successful')
  9. while tag:
  10. print("""
  11. 0 退出
  12. 1 取款
  13. 2 转账
  14. 3 查询
  15. """)
  16. choice=input('请输入您要执行的操作:') #choice='1'
  17. if choice == '':
  18. tag=False
  19. elif choice == '':
  20. print('取款。。。')
  21. elif choice == '':
  22. print('转账。。。')
  23. elif choice == '':
  24. print('查询')
  25. else:
  26. print('输入指令错误,请重新输入')
  27. else:
  28. print('username or password error')

流程控制:for循环

  for循环的强大之处在于循环取值
  1. l=['a','b','c','d','e']
  2.  
  3. i=0
  4. while i < len(l):
  5. print(l[i])
  6. i+=1
  7.  
  8. for x in l: # x='b'
  9. print(x)
  10.  
  11. dic={'name':'egon','age':18,'gender':'male'}
  12. for x in dic:
  13. print(x,dic[x])
for + break
  1. nums=[11,22,33,44,55]
  2. for x in nums:
  3. if x == 44:
  4. break
  5. print(x)
for + continue
  1. nums=[11,22,33,44,55]
  2. for x in nums:
  3. if x == 22 or x == 44:
  4. continue
  5. print(x)
for + else

break打断,则for循环同一级的后续代码将不再执行:else分支

  1. names=['egon','kevin1111_dsb','alex_dsb','mac_dsb']
  2.  
  3. for name in names:
  4. if name == 'kevin_dsb':
  5. break
  6. print(name)
  7. else:
  8. print('======>')
for+ range()
  1. range的用法
  2. >>> range(1,5)
  3. [1, 2, 3, 4]
  4. >>> for i in range(1,5):
  5. ... print(i)
  6. ...
  7. 1
  8. 2
  9. 3
  10. 4
  11. >>> range(1,5,1)
  12. [1, 2, 3, 4]
  13. >>> range(1,5,2) # 1 3
  14. [1, 3]
  1. for i in range(5): # 0 1 2 3 4
    print(i)
for嵌套
  1. for嵌套
  2. for i in range(3):
  3. for j in range(4):
  4. print(i,j)
  5. 执行结果:
  6. 0 0
  7. 0 1
  8. 0 2
  9. 0 3
  10.  
  11. 1 0
  12. 1 1
  13. 1 2
  14. 1 3
  15.  
  16. 2 0
  17. 2 1
  18. 2 2
  19. 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. HanLP 关键词提取算法分析

    HanLP 关键词提取算法分析 参考论文:<TextRank: Bringing Order into Texts> TextRank算法提取关键词的Java实现 TextRank算法自动 ...

  2. MongoDB 更新数组中的元素

    本文记录如何更新MongoDB Collection 中的Array 中的元素.假设Collection中一条记录格式如下: 现要删除scores 数组中,"type" 为 &qu ...

  3. Filter Authentication 登录认证

    [编程式配置]可用webxml配置替换@WebListenerpublic class FilterListenerConfigurator implements ServletContextList ...

  4. web 安全知识点

    XSS 新手指南:DVWA-1.9全级别教程(完结篇,附实例)之XSS https://www.jianshu.com/p/303206ae2471 https://www.netsparker.co ...

  5. python后端从数据库请求数据给到前端的具体实现

    先来贴一窜代码让大家理解前端/后端/数据库的工作原理, 首先简要说明:前端向后端请求数据,后端根据前端请求数据的类别分析其需求,并连接到数据库获取相应数据: 来一段简单的实例代码模拟淘宝商城: 前端代 ...

  6. 影子节点 shadowDOM

    示例: <video controls autoplay name="media"> <source id="mp4" src="t ...

  7. 简单重写容器vector

    #pragma once #include <iostream> using namespace std; template<class T> class CMyVector ...

  8. python基础知识~备份还原功能设计

    一 相关模块设计 1 配置文件管理模块 configparser    1 config-value不要加引号 2 linux系统执行命令模块 subprocess   1 shell=True代表命 ...

  9. Day17总结

    1:登录注册案例(理解) 2:Set集合(理解) (1)Set集合的特点 无序,唯一 (2)HashSet集合(掌握) A:底层数据结构是哈希表(是一个元素为链表的数组) B:哈希表底层依赖两个方法: ...

  10. 前端必备——js中前端与后台的数据交互全解

    只要编程语言能够支持网卡端口的监听和发送,理论上都是可以实现服务器后台设计的.也因此造成了实现后台的语言偏多,而web前端语言以html/css/js为主.所以在这里我们不涉及后台的设计,只介绍在we ...