《笨方法学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 ...
随机推荐
- 类名:IExternalCommandAvailability+IExternalCommand实现对某些控件的自定义使用
起初对于这些名词不懂,后经查阅了解如下,希望对学习者能有所帮助.在Revil里大部分命令在没有打开文档的时候是禁用的,有的在没有打开文档也是可以使用的.而又一些在平面视图是禁用的如标高,有的在3D视图 ...
- Android studio3.0打开Device File Explore(文件管理器)的方法(图文教程)
Android studio3.0打开Device File Explore(文件管理器)的方法(图文教程) 看到网上AS3.0新增加的查看手机文件的新功能,全部都是转载的,没有几个人心细的把如何打开 ...
- ASP.NET MVC 3 Application Upgrader
ASP.NET MVC 3 Application Upgrader: http://aspnet.codeplex.com/releases/view/59008
- Exce 快捷键 tips
1. 填充快捷键 ctrl+R 向下填充 CTRL+D 向右填充 2. 筛选快捷键 CTRL+SHIFT+L 3. 移动到当前区域的边缘: Ctrl + shift + 方向箭头 4. 字符连接:& ...
- sqlPlus基本操作
SQL*PLUS连接方法 1. $ sqlplus "user/password[@service] [as sysdba]" 2. $ sqlplus /nolog SQL> ...
- Caused by: javax.persistence.NonUniqueResultException: result returns more than one elements
Caused by: javax.persistence.NonUniqueResultException: result returns more than one elements at org. ...
- python初学心得之一
昨天开始接触并学习python,对python有了初步印象. 一.python主要应用方向 二.python语言类型 三.python2和3的主要区别 四.常见字符编码 五.Python语法初学 一 ...
- Swoft 缓存及Redis使用
配置 修改 /config/properties/cache.php 文件 return [ 'redis' => [ 'name' => 'redis', 'uri' => [ ' ...
- JavaScript学习-5——异步同步、回调函数
----------异步同步函数 ----------回调函数 一.异步同步函数 同步:发送一个请求,等待返回,然后再发送下一个请求 异步:发送一个请求,不等待返回,随时可以再发送下一个请求 同步可以 ...
- 二、http request:消息结构
1.request结构分为三部分: 抽象的东西,难以理解,老感觉是虚的, 所谓眼见为实, 实际见到的东西,我们才能理解和记忆,下面我们用fiddler打开一个博客园的地址,在Inspectors ta ...