python3xiaobaike_2020/chapter2/2-15 python3小白课:逻辑运算符.md
2025-04-20 23:22:14 +08:00

32 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# python3小白课逻辑运算符
逻辑运算主要用于操作布尔类型bool的变量、常量或表达式逻辑运算符的返回值还是布尔类型。许多编程语言都有相类似或相同的概念掌握一次学习其他编程语言时即可很快上手。
python中就三个逻辑运算符我们来直接看下代码例子
```python
# coding:utf-8
# and前后的表达式都为真时逻辑返回结果才为真True否则返回假False
a = 10
b = 5
print(a <= 10 and b == 5)
# or前后的表达式任意一个为真时逻辑返回结果即为真True都为假才返回假False
print(a >= 100 or b == 5)
# not只需要一个表达式表达式结果为True则返回False结果为False则返回True即反义
print(not(a == 10))
# 多个逻辑运算符还能嵌套运行,虽然有运算符优先级,但还是建议使用小括号把需要先算的括起来
# 这样可以保证代码的可读性,因为,一般人都记不住很多运算符的优先级的
print(a <= 10 and (a > b or b == 5))
```
关于逻辑运算符是大家必须掌握的东西,今后在做一些逻辑判断的时候很重要。
### 单词释义
| 单词 | 释义 |
| ---- | ---------------------- |
| and | 与 |
| or | 或 |
| not | 非,你也可以理解为不是 |