《笨方法学Python》加分题29
加分练习
猜一猜 “if 语句” 是什么,他有什么作用。在做下一道题之前,试着用自己的话回答下面的问题:
你认为 if 对他下一行代码做了什么?
为什么 if 语句的下一行需要 4 个空格缩进?
如果不缩进,会发生什么事情?
把练习 27 中的其它布尔表达式放到 if 语句中会不会也可以运行呢?试一下。
如果把变量 people, cats 和 dogs 的初始值改掉,会发生什么事情?
people = 20
cats = 30
dogs = 15 if people < cats:
print("Too many cats! The world is doomed!") if people > cats:
print("Not many cats! The world is saved!") if people < dogs:
print("The world is drooled on!") if people > dogs:
print("The world is dry!") dogs += 5 if people >= dogs:
print("People are greater than or equal to dogs.") if people <= dogs:
print("People are less than or equal to dogs.") if people == dogs:
print("People are dogs.")
从打字上来说这一题挺简单的,不过重点在于理解 if 语句的使用。跑一下结果如下。
29.1 if 语句的作用
分析一下前四段 if 语句可以发现 if 语句的作用
if 语句会根据其中语句的布尔值(True、False)影响其下一行代码是否执行。
如果是真 (if something Ture),就执行下面的代码。否则不执行。
29.2 为什么 if 语句下面一行的代码需要 4 个空格? + 29.3 如果不缩进会怎样?
这和我们在函数里面遇到的情况一样,4 个空格表示了哪些代码属于此条 if 语句。
a = 1
b = 2
c = 3 if a < b:
print("这是第一行")
print("这是第二行")
if c < a:
print("这是第三行")
print("这是第四行")
print("这是第五行") print("-" * 10)
print("反过来条件试一下") if a > b:
print("这是第一行")
print("这是第二行")
if c > a:
print("这是第三行")
print("这是第四行")
print("这是第五行")
可以看到,没有缩进的第五行是不受 if 语句影响的,而在缩进中的部分是否执行则在于 if 语句的真伪。
29.4 把 27 题改 if 语句
print("Is 'not False' True?")
if not False:
print("Yes! is True!")
print("\n------------------------")
print("Is 'not True' True?")
if not True:
print("Yes! is True!")
print("\n------------------------")
print("Is 'True or True' True?")
if True or True:
print("Yes! is True!")
print("\n------------------------")
print("Is 'True or False' True?")
if True or False:
print("Yes! is True!")
print("\n------------------------")
print("Is 'False or True' True?")
if False or True:
print("Yes! is True!")
print("\n------------------------")
print("Is 'False or False' True?")
if False or False:
print("Yes! is True!")
print("\n------------------------")
print("Is 'True and True' True?")
if True and True:
print("Yes! is True!")
print("\n------------------------")
print("Is 'True and False' True?")
if True and False:
print("Yes! is True!")
print("\n------------------------")
print("Is 'False and True' True?")
if False and True:
print("Yes! is True!")
print("\n------------------------")
print("Is 'False and False' True?")
if False and False:
print("Yes! is True!")
print("\n------------------------")
print("Is 'not (True or True)' True?")
if not (True or True):
print("Yes! is True!")
print("\n------------------------")
print("Is 'not (True or False)' True?")
if not (True or False):
print("Yes! is True!")
print("\n------------------------")
print("Is 'not (False or True)' True?")
if not (False or True):
print("Yes! is True!")
print("\n------------------------")
print("Is 'not (False or False)' True?")
if not (False or False):
print("Yes! is True!")
print("\n------------------------")
print("Is 'not (True and True)' True?")
if not (True and True):
print("Yes! is True!")
print("\n------------------------")
print("Is 'not (True and False)' True?")
if not (True and False):
print("Yes! is True!")
print("\n------------------------")
print("Is 'not (False and True)' True?")
if not (False and True):
print("Yes! is True!")
print("\n------------------------")
print("Is 'not (False and False)' True?")
if not (False and False):
print("Yes! is True!")
print("\n------------------------")
print("Is '1 != 1' True?")
if 1 != 1:
print("Yes! is True!")
print("\n------------------------")
print("Is '1 != 0' True?")
if 1 != 0:
print("Yes! is True!")
print("\n------------------------")
print("Is '0 != 1' True?")
if 0 != 1:
print("Yes! is True!")
print("\n------------------------")
print("Is '0 != 0' True?")
if 0 != 0:
print("Yes! is True!")
print("\n------------------------")
print("Is '1 == 1' True?")
if 1 == 1:
print("Yes! is True!")
print("\n------------------------")
print("Is '1 == 0' True?")
if 1 == 0:
print("Yes! is True!")
print("\n------------------------")
print("Is '0 == 1' True?")
if 0 == 1:
print("Yes! is True!")
print("\n------------------------")
print("Is '0 == 0' True?")
if 0 == 0:
print("Yes! is True!")
《笨方法学Python》加分题29的更多相关文章
- "笨方法学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》加分题28
#!usr/bin/python # -*-coding:utf-8-*- True and True print ("True") False and True print (& ...
- 《笨方法学Python》加分题16
基础部分 # 载入 sys.argv 模块,以获取脚本运行参数. from sys import argv # 将 argv 解包,并将脚本名赋值给变量 script :将参数赋值给变量 filena ...
随机推荐
- Android 开发 RecyclerView设置间距
实现步骤 首先要创建一个类继承RecyclerView.ItemDecoration 然后重新这个类的getItemOffsets方法,删除方法里的super.getItemOffsets(outRe ...
- docx httpheader头设置
设置contentType内容类型如下: Extension MIME Type .doc application/msword .dot application/msword .docx appli ...
- App专项测试之弱网测试
转载 https://blog.csdn.net/TestingGDR/article/details/83059415
- EFM32之GPIO
配置时钟: void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable) CMU_ClockEnable(cmuClock_HFPER, tru ...
- JSON Web Token(缩写 JWT) 目前最流行的跨域认证解决方案
一.跨域认证的问题 互联网服务离不开用户认证.一般流程是下面这样. 1.用户向服务器发送用户名和密码. 2.服务器验证通过后,在当前对话(session)里面保存相关数据,比如用户角色.登录时间等等. ...
- 1503.02531-Distilling the Knowledge in a Neural Network.md
原来交叉熵还有一个tempature,这个tempature有如下的定义: \[ q_i=\frac{e^{z_i/T}}{\sum_j{e^{z_j/T}}} \] 其中T就是tempature,一 ...
- inpuy只能输入数字,并且禁止输入e
<el-input type="number" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" ...
- 转载:C# socket端口复用-多主机头绑定
什么是端口复用: 因为在winsock的实现中,对于服务器的绑定是可以多重绑定的,在确定多重绑定使用谁的时候,根据一条原则是谁的指定最明确则将包递交给谁,而且没有权限之分.这种多重绑定便称之为端口复用 ...
- windows安装tf
https://www.cnblogs.com/lvsling/p/8672404.html
- C# webapi简单学习
创建WebApi项目: 在VS工具中创建一个ASP.NET Web应用程序 选择Webapi 一个webapi项目就创建好了 这里简单的写一个post和get两种请求的方法,由于post请求参数需要参 ...