#!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 了。不过还是要确保整个语句都能正常处理,以方便日后理解和使用代码。

《笨方法学Python》加分题28的更多相关文章

  1. "笨方法学python"

    <笨方法学python>.感觉里面的方法还可以.新手可以看看... 本书可以:教会你编程新手三种最重要的技能:读和写.注重细节.发现不同.

  2. 笨方法学python 22,前期知识点总结

    对笨方法学python,前22讲自己的模糊的单词.函数进行梳理总结如下: 单词.函数 含义 print() 打印内容到屏幕 IDLE 是一个纯Python下自带的简洁的集成开发环境 variable ...

  3. 笨办法学python 13题:pycharm 运行

    笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...

  4. 《笨方法学Python》加分题20

    加分练习通读脚本,在每一行之前加注解,以理解脚本里发生的事情.每次 print_a_line 运行时,你都传递了一个叫 current_line 的变量,在每次调用时,打印出 current_line ...

  5. 《笨方法学Python》加分题17

    题目通过前学习的文件操作把一个文件中的内容拷贝到另一个文件中,并使用 os.path.exists 在拷贝前判断被拷贝的文件是否已经存在,之后由用户判断是否继续完成拷贝. 新知识os.path.exi ...

  6. 《笨方法学Python》加分题15

    本题本题开始涉及文件的操作,文件操作是一件危险的事情,需要仔细细心否则可能导致重要的文件损坏. 本题除了 ex15.py 这个脚本以外,还需要一个用来读取的文件 ex15_sample.txt 其内容 ...

  7. 《笨方法学Python》加分题33

    while-leep 和我们接触过的 for-loop 类似,它们都会判断一个布尔表达式的真伪.也和 for 循环一样我们需要注意缩进,后续的练习会偏重这方面的练习.不同点在于 while 循环在执行 ...

  8. 《笨方法学Python》加分题29

    加分练习猜一猜 “if 语句” 是什么,他有什么作用.在做下一道题之前,试着用自己的话回答下面的问题: 你认为 if 对他下一行代码做了什么?为什么 if 语句的下一行需要 4 个空格缩进?如果不缩进 ...

  9. 《笨方法学Python》加分题16

    基础部分 # 载入 sys.argv 模块,以获取脚本运行参数. from sys import argv # 将 argv 解包,并将脚本名赋值给变量 script :将参数赋值给变量 filena ...

随机推荐

  1. python 平衡二叉树实现

    平衡二叉树: 在上一节二叉树的基础上我们实现,如何将生成平衡的二叉树 所谓平衡二叉树: 我自己定义就是:任何一个节点的左高度和右高度的差的绝对值都小于2 如图所示,此时a的左高度等于3,有高度等于1, ...

  2. scrpy-cookie

    两种方法模拟登陆 1.直接携带cookie import re import scrapy class RenrenSpider(scrapy.Spider): name = 'renren' all ...

  3. python-给微信好友自动发送天气预报和每日一句

    周末在宿舍学习python,女朋友那突然下了倾盆大雨,在图书馆门口跟我抱怨好久.最近又在学习python,就想给女朋友写个小程序,每天早上将每天的天气预报通过微信发个她. 在本程序中,用到了几个重要的 ...

  4. Django05-模型系统model

    Object Relational Mapping(ORM) 一.ORM介绍 1.ORM概念对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关 ...

  5. MATLAB 出一张好看的图

    1.坐标轴的视点(viewpoint):从哪个方向看整个坐标系统,这决定了坐标轴的方向和位置,通过view函数实现视点的设置:view([z y ]):(将坐标系统想象为一座房子,而自己是个会飞的天使 ...

  6. php+mysql 原生事务回滚

    <?php $conn = mysql_connect('127.0.0.1', 'root', ''); mysql_select_db('msc_test'); mysql_query('S ...

  7. 沙箱机制(Sandboxie)

    一.沙箱是什么? 沙箱是一个虚拟系统程序,沙箱提供的环境相对于每一个运行的程序都是独立的,而且不会对现有的系统产生影响. 二.沙箱的应用 (1)搭建测试环境.沙箱的应用只能访问自己的应用访问目录,而不 ...

  8. Powser Design 16.5 导入Mysql数据库的bug

    在Power Designer 16.5中,想导入mysql的某个数据库,选择导入后会导入全部数据库. 解决方案: 打开powerdesigner.选择file--->reverse engin ...

  9. rest_framework_extensions实现缓存

    1.安装包 pip install drf-extensions pip install django-redis pip install django-redis-cache 2.配置redis # ...

  10. Lua + Redis 解决高并发

    一.业务背景 优惠券业务主要提供用户领券和消券的功能:领取优惠券的动作由用户直接发起,由于资源有限,我们必须对用户的领取动作进行一些常规约束. 约束1(优惠券维度): 券的最大数量 max: 约束2( ...