if语句

  • 1.简单示例
  • 2.条件测试
    • 2.1是否相等(==)
    • 2.2是否不相等(!=)
    • 2.3比较数字
    • 2.4检查多个条件
      • 2.4.1使用and
      • 2.4.2使用or
    • 2.5是否包含在列表中(in)
    • 2.6是否不包含在列表中(not in)
    • 2.7布尔表达式
  • 3. if语句
    • 3.1简单的if语句
    • 3.2if-else语句
    • 3.3if-elif-else语句
    • 3.4多elif代码块
    • 3.5省略else代码块
    • 3.6测试多个条件
  • 4.使用if语句处理列表
    • 4.1检查特殊元素
    • 4.2确定列表不是空的
    • 4.3使用多个列表

1.简单示例

2.条件测试

2.1是否相等(==)

检查是否相等时区分大小写。

car='bmw'
car=='bmw'
True
car=='audi'
False

2.2是否不相等(!=)

requested_topping='mushrooms'
if requested_topping != 'anchovies':print("Hold the anchovies")
#Hold the anchovies

2.3比较数字

== 等于
!= 不等于
< 小于 > 大于
<=小于等于 >=大于等于

2.4检查多个条件

2.4.1使用and

只要一个没通过,整个表达式就为False

age_0=22
age_1=18
age_0>=21 and age_1>=21
#False

2.4.2使用or

只要一个满足,整个通过。

age_0>=21 or age_1>=21
#True

2.5是否包含在列表中(in)

requested_toppings=['mushrooms','onions','pineapple']
'mushrooms' in requested_toppings
#True
'pepperoni' in requested_toppings
#False

2.6是否不包含在列表中(not in)

banned_users=['andrew','carolina','david']
user='marie'
if user not in banned_users:print(user.title()+", you can post a responce if you wish.")
#Marie, you can post a responce if you wish.

2.7布尔表达式

条件测试的别名,结果要么是True,要么是False.

3. if语句

3.1简单的if语句

age=19
if age>=18:print("You are old enough to vote!")print("Have you registered to vote yet?")
#You are old enough to vote!
#Have you registered to vote yet?

3.2if-else语句

条件测试通过时执行一个操作,并没有通过时执行另一个操作。

age=17
if age>=18:print("You are old enough to vote!")print("Have you registered to vote yet?")
else:print("Sorry, you are too young to vote.")print("Please register to vote as soon as you turn 18.")
#Sorry, you are too young to vote.
#Please register to vote as soon as you turn 18.

3.3if-elif-else语句

需要检查超过两个的情形,可以使用Python提供的if-elif-else结构。Python只执行其中一个代码块,执行紧跟在它后面的代码,并跳过余下的测试。

age=12
if age<4:price=0
elif age<18:price=5
else:price=10
print("You admission cost is $"+str(price)+".")
#You admission cost is $5.

3.4多elif代码块

可根据需要使用任意数量的elif代码块。

age=66
if age<4:price=0
elif age<18:price=5
elif age<65:price=10
else:price=5
print("You admission cost is $"+str(price)+".")
#You admission cost is $5.

3.5省略else代码块

并不要求if-elif结构后面必须有else代码块,可用elif代替。

age=66
if age<4:price=0
elif age<18:price=5
elif age<65:price=10
elif age>=65:price=5
print("You admission cost is $"+str(price)+".")
#You admission cost is $5.

3.6测试多个条件

if-elif-else结构仅适合只有一个条件满足的情况,遇到通过的测试后,Python就跳过余下的测试。
如果必须检查所有条件,应使用一系列if语句。

requested_toppings=['mushrooms','extra cheese']if 'mushrooms' in requested_toppings:print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:print("Adding extra cheese.")print("\nFinish making your pizza!")
结果
Adding mushrooms.
Adding extra cheese.Finish making your pizza!

4.使用if语句处理列表

结合使用if语句和列表。

4.1检查特殊元素

requested_toppings=['mushrooms','green peppers','extra cheese']for requested_topping in requested_toppings:if requested_topping=='green peppers':print("Sorry, we are out of green pepper right now.")else:print("Adding "+requested_topping+".")print("\nFinished making your pizza.")
#结果
Adding mushrooms.
Sorry, we are out of green pepper right now.
Adding extra cheese.Finished making your pizza.

4.2确定列表不是空的

在运行for循环前,确定列表是否为空很重要。
在if语句中将列表名用在条件表达式中,Python将在列表至少包含一个元素时返回True,并在列表为空时返回False.

requested_toppings=[]
if requested_toppings:for requested_topping in requested_toppings:print("Adding "+requested_topping+".")print("\nFinished making your pizza.")
else:print("Are you srue you want a plain pizza?")
#Are you srue you want a plain pizza?

4.3使用多个列表

available_toppings=['mushrooms','olives','green peppers','pepperoni','pineapple','extra cheese']
requested_toppings=['mushrooms','french fries','extra cheese']for requested_topping in requested_toppings:if requested_topping in available_toppings:print("Adding "+requested_topping+".")else: print("Sorry, we don't have"+requested_topping+".")print("\nFinished making your pizza.")
#结果
Adding mushrooms.
Sorry, we don't havefrench fries.
Adding extra cheese.Finished making your pizza.

【Python笔记】第5章 if语句相关推荐

  1. 《Python从入门到实践》读书笔记——第五章 if语句

    <Python从入门到实践>读书笔记--第五章 if语句 1. 一个简单示例 cars = ['audi', 'bwm', 'subaru', 'toyota']for car in ca ...

  2. 《Python编程:从入门到实战》(第2版)学习笔记 第5章 if语句

    [写在前面]为进一步提高自己的python代码能力,打算把几本经典书籍重新过一遍,形成系统的知识体系,同时适当记录一些学习笔记,我尽量及时更新!先从经典的<Python编程:从入门到实战> ...

  3. Python程序开发——第二章 条件语句和循环语句

    目录 前言 一.if语句 (一)单分支 (二)双分支 (三)多分支 (四)if语句的嵌套 二.for循环 (一)for循环的定义 (二)range()函数控制循环次数 (三)for循环中的else子句 ...

  4. python笔记(三):if循环语句

     -----学习视频来源于马士兵教育----- 内容主要为了自己复习用,也可以让大家梳洗思路 ###if循环 money=1000 mrmb=int(input('输入取款金额:')) if mone ...

  5. PYTHON笔记第十一章之pygame.mixer

    from pygame import mixer#从PYGAME库导入MIXEWR包用来放声音 mixer.init()#假惺惺初始化一下 sound=mixer.Sound("qq.wav ...

  6. 【Python学习笔记】第一章基础知识:格式化输出,转义字符,变量类型转换,算术运算符,运算符优先级和赋值运算符,逻辑运算符,世界杯案例题目,条件判断if语句,猜拳游戏与三目运算符

    Python学习笔记之[第一章]基础知识 前言: 一.格式化输出 1.基本格式: 2.练习代码: 二.转义字符 1.基本格式: 2.练习代码: 3.输出结果: 三.输入 1.基本格式: 2.练习代码: ...

  7. 《Python编程:从入门到实践》读书笔记——第5章:if语句

    目录 条件测试 检查是否相等 检查是否不相等 数字比较 检查多个条件 检查特定值是否包含在列表中 检查特定值是否不包含在列表中 布尔表达式 if语句 简单的if语句 if-else语句 if-elif ...

  8. 读书笔记:《流畅的Python》第五章 一等函数

    # 一等对象/一等函数 ''' 1.在运行时创建 2.能赋值给变量或数据结构中的元素 3.能作为函数的参数传给函数 4.能作为函数的返回值返回结果 '''# 函数对象本身时function对象的实例d ...

  9. 《Python从入门到实践》读书笔记——第六章 字典

    <Python从入门到实践>读书笔记--第六章 字典 1. 一个简单的字典 alien_0 = {'color': 'green', 'points': 5}print(alien_0[' ...

  10. Python基础——第四章:Python循环语句

    前言 本文是根据黑马程序员Python教程所作之笔记,目的是为了方便我本人以及广大同学们查漏补缺. 不想做笔记直接来我的频道.当然啦,自己的笔记才是最好的哦! PS:感谢黑马程序员! 教程链接:黑马程 ...

最新文章

  1. python哨兵循环_Python通用循环的构造方法实例分析
  2. GNOME启动时激活NumLock
  3. Lotus Notes常见问题答疑
  4. MySQL 数据库sql命令查看表属性,mysql查看指定表的各字段最大值、是否为空等属性实例演示
  5. 将长度为n的绳子分为m段求各段乘积的最大值
  6. IE9 Preview 4的CSS3支持。
  7. net.conn read 判断数据读取完毕_高并发:缓存模式以及缓存的数据一致性
  8. Python enumerate 函数 - Python零基础入门教程
  9. loadrunner 脚本优化-事务时间简介
  10. 第 39 章 ThinkPHP--模型初步(下)
  11. 综述 | 跨语言自然语言处理论文汇总
  12. Linux内核入门(七)—— 必要的编译知识
  13. linux下命令打开url,在linux命令下访问url
  14. Source Map的概念
  15. globk命令帮助信息
  16. 微信公众号 配网 airkiss配网 wifi配网
  17. 茅侃侃自述离职Majoy经过:让我们从“失败”开始
  18. Flink1.12源码解读——Checkpoint详细执行过程
  19. matlab产生正定矩阵
  20. [zz]澄清P问题、NP问题、NPC问题的概念

热门文章

  1. 3.19美团实习面试一面二面(已offer)
  2. ebyte Lora 转 4G 透传通讯测试
  3. 如何避免服务器被恶意网络攻击
  4. bch verilog代码_BCH源码学习笔记 | 第一步:搭建BCH的源码学习环境
  5. python3-欢乐斗牛-实战
  6. 【quick-cocos2d-lua】 疯狂牛牛
  7. 台式计算机按住开关风扇才能转,台式机风扇转但不开机的解决方法
  8. 复权不复权,天差与地别 | 量化投资中如何最准确的计算股票前后复权价(附代码)
  9. 几款任意波形发生卡推荐
  10. C语言数据结构队列的插入和删除