是一种特殊的数字类型,它只有两个值:True和False。它们用于逻辑判断和条件分支。
bool(None)
False
import fractions
bool(fractions.Fraction())
False
bool([])
False
Python 逻辑运算符包括:and(与)、or(或)、not(非)。
1. and运算符:只有在两个条件都为True时,整个条件才会被视为True
x = 5
y = 10
z = 15
if x < y and y < z:
print("Both conditions are True")
输出结果为:
Both conditions are True
2. or运算符:只有在两个条件中至少有一个为True时,结果为True
x = 5
y = 10
z = 15
if x > y or y > z:
print("One or both conditions are True")
输出结果为:
One or both conditions are True
3. not运算符:对条件的结果取反
not True
False
| 运算符 | 描述 |
|---|---|
| ** | 指数 |
| +x -x | 正负号 |
| * / // % | 乘法,除法,地板除,取余数 |
| + - | 加法,减法 |
| in, not in, is, is not, < , <=, !=, == | 同一性测试,比较 |
| not | 非 |
| and | 与 |
| or | 或 |
| if-else | 条件表达式 |
3 and 5 + True or False
6
#运行3 and(5 + True)or False