逻辑运算:
同时判断多个条件,当多个条件都满足才继续执行后面的代码, 就用到了逻辑运算符 and、 or、 not 这三种
逻辑运算符可以把 多个条件 按照 逻辑 进行连接 变成 更复杂的条件。
1. 与 或 非的运算
运算符 | 逻辑表达式 | 描述 |
and | 条件1 and 条件2 | 只有条件1 和 条件2的值都为True, 才会返回True 否则只要条件1 或者 条件2有一个值为 False, 就返回False |
or | 条件1 or 条件2 |
只要 条件1 或者 条件2 有一个值为 True, 就返回True 只有 条件1 和 条件2 的值都为False, 才会返回False |
not | not 条件 | 如果 条件 为True, 则返回 False 如果 条件 为 False, 则返回 True |
2. 案例
1) and 案例 判断年龄是否合法
age = int(input("请输入年龄:"))
if age >= 0 and age <= 120:
print("年龄合法")
else:
print("年龄不合法")
结果: 请输入年龄:20
年龄合法
2) or 案例 java和python成绩
java_score=89
python_score=55
if java_score>= 60 or python_score >= 60:
print("及格")
else:
print("都不及格")
结果: 及格
3) not 案例
value=True
if not value:
print("非")
else:
print("是")
结果: 是