在Python里 逻辑运算符(and or not)和比较运算符(== != > <等)是两个不同的概念
(p.s. 这几个比较常见)
逻辑运算符用于连接多个比较运算符得到的布尔值 得到一个最终的布尔值
例如:
and:两边都为真(True) 结果为真(True);否则为假(False)
or:两边至少有一真(True) 结果为真(True);否则为假(False)
not:真(True)变为假(False) 假(False)变为真(True)
比较运算符用于比较两个值的关系 返回一个布尔值 例如等于(==)判断两边的值是否相等
如果
相等返回True
否则为False
不等于(!=)则相反
II. Suppose the value of b is False and value of x is 0. What is the value of each of the following expressions?
a. b and x == 0 b.
b or x == 0
c. not b and x == 0
d. not b or x != 0
题目给出的变量b的值为False 变量x的值为0
解题代码a
b and x == 0
b = False
x = 0
result = b and x == 0
print(result) # 输出:False
这里面x == 0 为 True 然后再对 b 和这个 True 值进行逻辑与运算 False and True 的结果为 False
解题代码b
b or x == 0
result = b or x == 0
print(result) # 输出:True
x == 0 为 True 然后再对 b 和这个 True 值进行逻辑或运算 False or True 的结果为True
not b and x == 0
result = not b and x == 0
print(result) # 输出:True
首先计算 not b b 为 False not False 的结果是 True 再对 True 和 x == 0(结果为 True)进行逻辑与 True and True 的结果是True
not b or x != 0
result = not b or x != 0
print(result) # 输出:True
首先计算not b b为 False not False 的结果是True再对 True 和 x != 0(结果为 False)进行逻辑或True or False 的结果是 True
III.Simplify the following expressions. Assume the variable b is a boolean variable.
a. b == True
b. b == False
c. b != True
d. b != False
这四个问题都可以通过Python的逻辑运算符进行简化
首先b == True和b实际上是一个意思
b == False对应的是not b 相当于否定的b
a) b == True
这个表达式直接可以简化为 b 因为如果 b是真那么 b == True 就返回True 否则返回False 等价于 b 本身
b) b == False
可以简化为 not b 因为b == False 在 b 为真的情况下返回 False 在b为假的情况下返回 True 与 not b 的逻辑相同
c) b != True
可以简化为 not b 它实际上是在描述 b 不是 True 也就是 b 是 False 这与 not b 的定义相同
d.) b != False
可以简化为 bb != False描述的是 b 不是 False也就是 b 是 True 这与 b 的定义相同
所以这四个表达式可以简化为:
a. b b. not b c. not b d. b
IIII. Write a program that asks the user to enter a month (1 for January, 2 for February, and
so on) and then prints the number of days in the month. For February, print ‘28 or 29 days’.
Sample output:
Enter a month: 5
30 days
IIIII. A year with 366 days is called a leap year. Leap years are necessary to keep the calendar synchronized with the sun because the earth revolves around the sun once every
365.25 days. Actually, that figure is not entirely precise, and for all dates after 1582 the
Gregorian correction applies. Usually years that are divisible by 4 are leap years (for
example, 1996). However, years that are divisible by 100 (for example, 1900) are not
leap years but years that are divisible by 400 are leap years (for example, 2000). Write
a program that asks the user for a year and computes whether that year is a leap year.
Use a single if-statement and logic operators.
首先解题思路是:
graph TD
A[year能被4整除] -->|是| B[year不能被100整除]
A -->|否| D[不是闰年]
B -->|是| C[是闰年]
B -->|否| E[year能被400整除]
E -->|是| F[是闰年]
E -->|否| G[不是闰年]
代码:
# 获取用户输入
year = int(input("请输入一个年份:"))
# 判断是否为闰年
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print(year, "是闰年")
else:
print(year, "不是闰年")
代码解析:
year % 4 == 0:用输入的年份 year 对4取余数 如果余数为0 就表示这个年份能被4整除
year % 100 != 0:用输入的年份 year 对100取余数 如果余数不为0 就表示这个年份不能被100整除
year % 400 == 0:用输入的年份 year 对400取余数 如果余数为0 就表示这个年份能被400整除
and 逻辑操作符用在 year % 4 == 0 and year % 100 != 0 部分 表示这两个判断条件都必须为真 结果才为真 也就是说 如果年份能被4整除且不能被100整除 这部分的结果就为真
or 逻辑操作符用在整体判断语句 (year % 4 == 0 and year % 100 != 0) or year % 400 == 0 部分 表示只要这两部分中的任一部分为真 整个判断结果就为真 也就是说 只要年份能被4整除且不能被100整除 或者能被400整除 就认定这个年份为闰年