“笨办法”学Python 3 ——练习 28 布尔练习

练习28 布尔表达式练习

知识点:

1.什么是布尔表达式?
布尔表达式是一段代码声明,它最终只有true(真)和false(假)两个取值
2. 赋值运算符

  • = :简单的赋值运算符;c = a + b 将 a + b 的运算结果赋值为 c
  • += :加法赋值运算符;c += a 等效于 c = c + a
  • -= :减法赋值运算符;c -= a 等效于 c = c - a
  • *= :乘法赋值运算符;c *= a 等效于 c = c * a
  • /= :除法赋值运算符;c /= a 等效于 c = c / a
  • %= :取模赋值运算符;c %= a 等效于 c = c % a,返回除法的余数。 如: 5 % 2 = 1
  • **= :幂赋值运算符;c **= a 等效于 c = c ** a, 返回c的a次幂,如:3 ** 3 = 27
  • //= :取整除赋值运算符;c //= a 等效于 c = c // a,向下取整的除法,取浮点数的整数部分,例如3//2=1。

3.布尔表达式的分析方式:
(1)把每一个相等性测试(== 或者 !=)替换成真实性测试。
(2)先解决圆括号里面的 and/or。
(3)找到每一个 not,然后把它反转过来。
(4)找到剩余的 and/or,然后解决掉
(5)当你完成的时候,你应该得到 True 或者 False

4.布尔表达式总结:
任何包含一个 False 的 and 表达式结果都是 False。任何包含一个 True 的 or 表达式结果都是 True。但是你要掌握处理整个表达式的过程,后面会用到。

手动练习

  1. True and True : True
  2. False and True :False
  3. 1 == 1 and 2 == 1 : False
  4. “test” == “test” : True
  5. 1 == 1 or 2 != 1 : True
  6. True and 1 == 1 : True
  7. False and 0 != 0 : False
  8. True or 1 == 1 : True
  9. “test” == “testing” : False
  10. 1 != 0 and 2 == 1 : False
  11. “test” != “testing” : True
  12. “test” == 1 : False
  13. not (True and False) : True
  14. not (1 == 1 and 0 != 1) : False
  15. not (10 == 1 or 1000 == 1000) :False
  16. not (1 != 10 or 3 == 4) : False
  17. not (“testing” == “testing” and “Zed” == “Cool Guy”) : True
  18. 1 == 1 and (not (“testing” == 1 or 1 == 0)) : True
  19. “chunky” == “bacon” and (not (3 == 4 or 3 == 3)) : False
  20. 3 == 3 and (not (“testing” == “testing” or “Python” == “Fun”)) : False
    全对。

终端运行

Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> True and True
True
>>> False and True
False
>>> 1 == 1 and 2 == 1
False
>>> "test" == "test"
True
>>> 1 == 1 or 2 != 1
True
>>> True and 1 == 1
True
>>> False and 0 != 0
False
>>> True or 1 == 1
True
>>> "test" == "testing"
False
>>> 1 != 0 and 2 == 1
False
>>> "test" != "testing"
True
>>> "test" == 1
False
>>> not (True and False)
True
>>> not (1 == 1 and 0 != 1)
False
>>> not (10 == 1 or 1000 == 1000)
False
>>> not (1 != 10 or 3 == 4)
False
>>> not ("testing" == "testing" and "Zed" == "Cool Guy")
True
>>> 1 == 1 and (not ("testing" == 1 or 1 == 0))
True
>>> "chunky" == "bacon" and (not (3 == 4 or 3 == 3))
False
>>> 3 == 3 and (not ("testing" == "testing" or "Python" == "Fun"))
False
>>> quit()

附加练习

  • Python 中有很多类似于 != 和 == 的运算符,试着尽可能多地找到这类“比较运算符”( equality operators),比如 < 或者 <=。写下这些比较运算符的名字,比如我们把 != 叫做“不等于”。
    比较(关系)运算符:

  • == : 等于

  • != :不等于

  • .>:大于

  • <:小于

  • .>= :大于等于

  • <= :小于等于

常见问题

  1. 为什么 “test” and “test” 返回的是 test,1 and 1 返回的是 1 而不是 True? Python 和其他很多语言喜欢返回布尔表达式的运算数而不是只是 True 或者 False。这意味着,如果是 False and 1,你会得到第一个运算数(False),如果是 True and 1,你会得到第二个运算数(1),试着玩玩这个。
    测试:
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> "test" and "test"
'test'
>>> 1 and True
True
>>> True and 1
1
>>> 1 and False
False
>>> False or 1
1

2.!= 和 <> 有区别吗?
Python 已经不提倡使用 <> ,而是更多地使用 !=,除此之外,二者没有任何区别。
3.见知识点4,布尔表达式总结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值