if statements if语句在编程中是极其重要的,允许我们建立能够基于某种条件做出决策的程序。如果某些条件是真的我们会做一些事情,否则我们会做其他事情。
1、涉及的知识点
(1)Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
(2)Python程序语言指定任何非0和非空(null)值为True,0 或者 null为False。
(3)Python 编程中 if 语句用于控制程序的执行,基本形式为:
1 2 3 4 if 判断条件: 执行语句…… else: 执行语句……
备注:其中”判断条件”成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围。else 为可选语句,当需要在条件不成立时执行内容则可以执行相关语句。
2、举例练习
(1)题目1
1 2 3 4 5 6 7 8 if it's hot It's a hot day Drink plenty of water otherwise if it's cold It's a cold day Wear warm clothes otherwise It's a lovely day
1 2 3 4 5 6 7 8 9 is_hot = False if is_hot: print("It's a hot day") print("Drink plenty of water") else: print("It's a cold day") print("Wear warm clothes") print("Enjoy your day")
备注:if语句写完回车后,光标会自动缩进,意味着可以继续编写更多代码;如果条件成立,这些代码将会全部执行,可以使用shift + tab取消缩进,即if语句下不在编写更多额外代码,以终止此块代码;if语句之外,不论条件是否成立,程序都会执行语句。
1 2 3 4 5 6 7 8 9 10 11 12 is_hot = False is_cold = False if is_hot: print("It's a hot day") print("Drink plenty of water") elif is_cold: print("It's a cold day") print("Wear warm clothes") else: print("It's a lovely day") print("Enjoy your day")
3、学习任务
(1)任务内容
1 2 3 4 5 6 7 8 Price of a house is $1M. If buyer has good credit, they need to put down 10% Otherwise they need to put down 20% Print the down payment 房子的价格是100万美元。如果购买者有好的信用,则首付只需要付全款的10%;否则,需要付全款的20%。
(2)解决方案
1 2 3 4 5 6 7 8 price = 1000000 has_good_credit = True if has_good_credit: down_payment = 0.1 * price else: down_payment = 0.2 * price print(f"Down Payment: ${down_payment}")
Logical Operators 逻辑运算符 1、涉及的知识点
Python语言支持逻辑运算符如下表所示,其中假设变量 a 为 10, b为 20:
运算符
逻辑表达式
描述
实例
and
x and y
布尔”与”:如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值
(a and b) 返回 20
or
x or y
布尔”或”:如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值
(a or b) 返回 10
not
not x
布尔”非”:如果 x 为 True,返回 False 。如果 x 为 False,它返回 True
not(a and b) 返回 False
2、举例练习
(1)题目1
1 2 3 if applicant has high income AND good credit Eligible for loan 如果申请人有高的收入和好的信用,则有资格获得贷款。
1 2 3 4 5 has_high_income = True has_good_credit = True if has_high_income and has_good_credit: print("Eligible for loan")
备注:两个条件都为真才为真
(2)题目2
1 2 3 if applicant has high income OR good credit Eligible for loan 如果申请人有高的收入或者好的信用,则有资格获得贷款。
1 2 3 4 5 has_high_income = True has_good_credit = False if has_high_income or has_good_credit: print("Eligible for loan")
备注:只要有一个条件为真即为真
(3)题目3
1 2 if applicant has good credit AND doesn't have a criminal record Eligible for loan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 第一种情况: has_good_credit = True has_criminal_record = False if has_good_credit and not has_criminal_record: print("Eligible for loan") # not 操作符会将False变成True 返回结果:打印Eligible for loan 第二种情况: has_good_credit = True has_criminal_record = True if has_good_credit and not has_criminal_record: print("Eligible for loan") # not 操作符会将True变成False 返回结果:不打印任何数据
(4)总结
AND:both,两个条件都为真才为真
OR::at least one,只要有一个条件为真即为真
NOT:将条件得到的布尔值取反,语句真假跟随and或者or
Comparison Operators 比较运算符 1、涉及的知识点
Python语言支持比较运算符如下表所示,其中假设变量a为10,变量b为20:
运算符
描述
实例
==
等于:比较对象是否相等
(a == b) 返回 False
!=
不等于:比较两个对象是否不相等
(a != b) 返回 true
>
大于:返回x是否大于y
(a > b) 返回 False
<
小于:返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价
(a < b) 返回 true
>=
大于等于:返回x是否大于等于y
(a >= b) 返回 False
<=
小于等于:返回x是否小于等于y
(a <= b) 返回 true
2、举例练习
(1)题目1
1 2 3 4 5 6 if temperature is greater than 30 it's a hot day otherwise if it's less than 10 it's a cold day otherwise it's neither hot nor cold
1 2 3 4 5 6 temperature = 30 if temperature > 30: print("It's a hot day") else: print("It's not a hot day")
(2)题目2
1 2 3 4 5 6 if name is less than 3 characters long name must be at least 3 characters otherwise if it's more than 50 characters long name can be a maximum of 50 characters otherwise name looks good!
1 2 3 4 5 6 7 8 name = "Zhang Zhiqing" if len(name) < 3: print("Name must be at least 3 characters.") elif len(name) > 50: print("Name can be a maximum of 50 characters.") else: print("Name looks good!")
3、学习任务:Weight Converter
(1)题目内容
先输入体重数值,再输入单位。如果输入的单位为千克,则转换为磅然后输出体重;如果输入的单位为磅,则转换为千克然后输出体重。
(2)解决方法
1 2 3 4 5 6 7 8 weight = int(input('Weight: ')) unit = input('(L)bs or (K)g: ') if unit.upper() == "L": converted = weight * 0.45 print(f"You are {converted} kilos") else: converted = weight / 0.45 print(f"You are {converted} pounds")
备注:为了防止因用户输入小写字母导致程序不起作用,我们使用点运算符调用方法函数upper(),此方法将用户输入的内容转换为大写。