《笨方法学Python》加分题28
#!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的更多相关文章
- "笨方法学python"
<笨方法学python>.感觉里面的方法还可以.新手可以看看... 本书可以:教会你编程新手三种最重要的技能:读和写.注重细节.发现不同.
- 笨方法学python 22,前期知识点总结
对笨方法学python,前22讲自己的模糊的单词.函数进行梳理总结如下: 单词.函数 含义 print() 打印内容到屏幕 IDLE 是一个纯Python下自带的简洁的集成开发环境 variable ...
- 笨办法学python 13题:pycharm 运行
笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...
- 《笨方法学Python》加分题20
加分练习通读脚本,在每一行之前加注解,以理解脚本里发生的事情.每次 print_a_line 运行时,你都传递了一个叫 current_line 的变量,在每次调用时,打印出 current_line ...
- 《笨方法学Python》加分题17
题目通过前学习的文件操作把一个文件中的内容拷贝到另一个文件中,并使用 os.path.exists 在拷贝前判断被拷贝的文件是否已经存在,之后由用户判断是否继续完成拷贝. 新知识os.path.exi ...
- 《笨方法学Python》加分题15
本题本题开始涉及文件的操作,文件操作是一件危险的事情,需要仔细细心否则可能导致重要的文件损坏. 本题除了 ex15.py 这个脚本以外,还需要一个用来读取的文件 ex15_sample.txt 其内容 ...
- 《笨方法学Python》加分题33
while-leep 和我们接触过的 for-loop 类似,它们都会判断一个布尔表达式的真伪.也和 for 循环一样我们需要注意缩进,后续的练习会偏重这方面的练习.不同点在于 while 循环在执行 ...
- 《笨方法学Python》加分题29
加分练习猜一猜 “if 语句” 是什么,他有什么作用.在做下一道题之前,试着用自己的话回答下面的问题: 你认为 if 对他下一行代码做了什么?为什么 if 语句的下一行需要 4 个空格缩进?如果不缩进 ...
- 《笨方法学Python》加分题16
基础部分 # 载入 sys.argv 模块,以获取脚本运行参数. from sys import argv # 将 argv 解包,并将脚本名赋值给变量 script :将参数赋值给变量 filena ...
随机推荐
- QPixmap 在非QtCreator环境下无法显示jpg图片
这几天需要实现在Qt界面中显示jpg图片,于是直接将路径传给QPixmap对象,发现显示不出来. 然而在Qt SDK自带的Demo中却可以正确显示jpg图片,经搜索引擎查找发现,是自己的exe文件缺少 ...
- ARCGIS10.5安装教程(附完整安装包和汉化包)
2017年7月,Esri公司发布了arcgis10.5.1版本,该版本的整体界面风格延续了10.0版本的界面风格,新功能介绍详见 http://www.3snews.net/column/252000 ...
- 使用Jquery easyui datagrid请求servlet没有反应的解决办法
在Jsp页面中把servlet请求地址写全,我已经将要注意的地方红色加粗了.我的jsp页面是新建的一个文件夹. <%@ page language="java" conten ...
- Java 8 默认方法
转自:https://www.runoob.com/java/java8-default-methods.html Java 8 新增了接口的默认方法. 简单说,默认方法就是接口可以有实现方法,而且不 ...
- C#控件——批量化隐藏或显示同类型控件
当一个页面中添加了许多同类型控件,当需要控制这些控件进行显示或隐藏的时候,需要一个个的将Visible属性设置为false,十分不方便, 后通过论坛受一位大神(至于叫什么忘了)的启发,通过建立控件数组 ...
- python之路:列表及元组之定义
python开发之路:列表及元组之定义 列表是以后用处较大的一个数据类型,这种数据类型可以存储按组分类的信息.好了,我不多说,开始讲了! 好了,现在我有个情景,我要存东汉时期(韩国,秦国,……)所 ...
- mysql查询正在执行的sql
mysql> SHOW VARIABLES LIKE "general_log%"; +------------------+------------------------ ...
- open()函数 linux中open函数使用
来源:http://www.cnblogs.com/songfeixiang/p/3733855.html linux中open函数使用 open函数用来打开一个设备,他返回的是一个整型变量,如果 ...
- shift() 方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。
let a = [1, 2, 3]; let b = a.shift(); console.log(a); // [2, 3] console.log(b); // 1 返回值 从数组中删除的元素; ...
- jquery中的 deferred之 when (三)
先来看使用案例: var def1 = $.Deferred(); var def2 = $.Deferred(); var def3 = $.Deferred(); var def4 = $.Def ...