运算符:
not and or
非 与 或
布尔非操作 not
语法
not x
作用:
对x进行布尔取非,如bool(x) 为True,则返回False,
否则为True
布尔与操作 and
语法:
x and y
注: x,y 代表表达式
作用:
优先返回假值对象,当x的布尔值为False时,返回x
否则返回y
示例:
True and True # True
True and False # False
False and True # False
False and False # False
100 and 0.0 # 0.0
0 and 0.0 # 0
布尔或操作 or
语法:
x or y
作用:
优先返回真值对象,当x为True时返回x,否则返回y
示例:
True or True # True
True or False # True
False or True # True
False or False # False
3.14 or 100 # 3.14
0 or 0.0 # 0.0
正负号运算符
+(正号) -负号
注: 一元运算符(一个元素参加运算)
语法:
+ 表达式
- 表达式
示例:
a = 5
b = -a # -负号
c = +a # 正号
print(a, b, c) # 5 -5 5
in, not in 运算符
作用:
in 用于序列,字典,集合中,用于判断某个值是否存
在于容器中,如果存在则返回True,则返回False
not in 与 in 运算符的返回结果相反
格式:
对象 in 序列
示例:
x = 'welcome to tarena!'
'to' in x # True
'hello' in x # False
'e t' in x # True
'abc' not in x # True