笨方法学Python 习题 28: 布尔表达式练习
2017-08-04 10:48
405 查看
#!usr/bin/python
# -*-coding:utf-8-*-
True and True
print ("True")
False and True
print ("False")
1 == 1 and 2 == 1
print ("False")
"test" == "test"
print ("True")
1 == 1 or 2 != 1
print ("True")
True and 1 == 1
print ("True")
False and 0 != 0
print ("False")
True or 1 == 1
print ("True")
"test" == "testing"
print ("False")
1 != 0 and 2 == 1
print ("False")
"test" != "testing"
print ("True")
"test" == 1
print ("False")
not (True and False)
print ("True")
not (1 == 1 and 0 != 1)
print ("False")
not (10 == 1 or 1000 == 1000)
print ("False")
not (1 != 10 or 3 == 4)
print ("False")
not ("testing" == "testing" and "Zed" == "Cool Guy")
print ("True")
1 == 1 and not ("testing" == 1 or 1 == 0)
print ("True")
"chunky" == "bacon" and not (3 == 4 or 3 == 3)
print ("False")
3 == 3 and not ("testing" == "testing" or "Python" == "Fun")
print ("False")运行结果如下:
>>> 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
>>>
加分习题
Python 里还有很多和 != 、 == 类似的操作符. 试着尽可能多地列出 Python 中的等价运算符。例如 < 或者 <= 就是。
写出每一个等价运算符的名称。例如 != 叫 “not equal(不等于)”。
== (equal) 等于
>= (greater-than-equal) 大于等于
<= (less-than-equal) 小于等于
在 python 中测试新的布尔操作。在敲回车前你需要喊出它的结果。不要思考,凭自己的第一感就可以了。把表达式和结果用笔写下来再敲回车,最后看自己做对多少,做错多少。
把习题 3 那张纸丢掉,以后你不再需要查询它了。
常见问题回答
为什么 "test" and "test" 返回 "test", 1 and 1 返回 1,而不是返回 True 呢?
Python 和很多语言一样,都是返回两个被操作对象中的一个,而非它们的布尔表达式 True 或 False 。这意味着如果你写了 False and 1 ,你得到的是第一个操作字元(False),而非第二个字元(1)。多多实验一下。
!= 和 <> 有何不同?
Python 中 <> 将被逐渐弃用, != 才是主流,除此以为没什么不同。
有没有短路逻辑?
有的。任何以 False 开头的 and 语句都会直接被处理成 False 并且不会继续检查后面语句了。任何包含 True 的 or 语句,只要处理到 True 这个字样,就不会继续向下推算,而是直接返回 True 了。不过还是要确保整个语句都能正常处理,以方便日后理解和使用代码。
# -*-coding:utf-8-*-
True and True
print ("True")
False and True
print ("False")
1 == 1 and 2 == 1
print ("False")
"test" == "test"
print ("True")
1 == 1 or 2 != 1
print ("True")
True and 1 == 1
print ("True")
False and 0 != 0
print ("False")
True or 1 == 1
print ("True")
"test" == "testing"
print ("False")
1 != 0 and 2 == 1
print ("False")
"test" != "testing"
print ("True")
"test" == 1
print ("False")
not (True and False)
print ("True")
not (1 == 1 and 0 != 1)
print ("False")
not (10 == 1 or 1000 == 1000)
print ("False")
not (1 != 10 or 3 == 4)
print ("False")
not ("testing" == "testing" and "Zed" == "Cool Guy")
print ("True")
1 == 1 and not ("testing" == 1 or 1 == 0)
print ("True")
"chunky" == "bacon" and not (3 == 4 or 3 == 3)
print ("False")
3 == 3 and not ("testing" == "testing" or "Python" == "Fun")
print ("False")运行结果如下:
>>> 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
>>>
加分习题
Python 里还有很多和 != 、 == 类似的操作符. 试着尽可能多地列出 Python 中的等价运算符。例如 < 或者 <= 就是。
写出每一个等价运算符的名称。例如 != 叫 “not equal(不等于)”。
== (equal) 等于
>= (greater-than-equal) 大于等于
<= (less-than-equal) 小于等于
在 python 中测试新的布尔操作。在敲回车前你需要喊出它的结果。不要思考,凭自己的第一感就可以了。把表达式和结果用笔写下来再敲回车,最后看自己做对多少,做错多少。
把习题 3 那张纸丢掉,以后你不再需要查询它了。
常见问题回答
为什么 "test" and "test" 返回 "test", 1 and 1 返回 1,而不是返回 True 呢?
Python 和很多语言一样,都是返回两个被操作对象中的一个,而非它们的布尔表达式 True 或 False 。这意味着如果你写了 False and 1 ,你得到的是第一个操作字元(False),而非第二个字元(1)。多多实验一下。
!= 和 <> 有何不同?
Python 中 <> 将被逐渐弃用, != 才是主流,除此以为没什么不同。
有没有短路逻辑?
有的。任何以 False 开头的 and 语句都会直接被处理成 False 并且不会继续检查后面语句了。任何包含 True 的 or 语句,只要处理到 True 这个字样,就不会继续向下推算,而是直接返回 True 了。不过还是要确保整个语句都能正常处理,以方便日后理解和使用代码。
相关文章推荐
- python 笔记 布尔表达式练习 《笨办法学Python》习题28 ——1.2
- 笨方法学Python 习题 24: 更多练习
- 笨方法学习Python-习题25: 更多更多的练习
- 习题28 布尔表达式练习
- 本方法学python 习题24(综合练习) 习题25(更多关于函数和变量的练习)习题26(修改程序)
- 笨方法学习Python-习题24: 更多练习
- 笨方法学python 习题27(逻辑之前的内容)习题28(布尔)29、30、31
- 笨方法学Python 习题 25: 更多更多的练习
- Python学习_06 习题练习
- Python学习笔记: 第五章课后习题练习
- [笨方法学python]习题51自动化测试笔记
- python 手记9 〖笨方法学python习题30〗
- PythonNLP学习进阶:习题练习(2016-2-12)
- 笨方法学Python 习题 3: 数字和数学计算
- 笨方法学Python 习题 9: 打印,打印,打印
- 笨方法学Python 习题 12: 提示别人
- 笨方法学习Python-习题20: 函数和文件
- 笨方法学Python 习题 15: 读取文件
- while循环和for循环的比较,习题33,笨方法学python
- 笨方法学习Python-习题26: 恭喜你,现在可以考试了!