运算关系

逻辑运算符

逻辑运算符用于连接多个条件,进行关联判断,会返回布尔值True或False

1.not

逻辑 非,也就是取反

偷懒原则:not 就是:真变假,假变真

print(not 1) #1在逻辑运算中代表True,not 1 就是 not True
False

print(not 0) #1在逻辑运算中代表False,not 0 就是 not False
True

2.and

逻辑 与

偷懒原则:and 就是:全真为真,一假即假

print(1 and 4>1 and True)
True

print(3>4 and 0 and False and 1)
False

3.or

逻辑 或

偷懒原则:or 就是:一真即真,全假为假

print(1 or 4>1 or True)
1 #1在逻辑运算中代表True

print(3>4 or 0 or False)
False

优先级

not > and > or

PS:如果单独就只是一串and连接,或者单独就只是一串or连接,按照从左到右的顺序运算

PS:如果是混用,则需要考虑优先级了()拥有最高优先级,“()”内的内容直接提升到第一优先级,先运算

成员运算符

1.in

判断一个字符或者字符串是否存在于一个大字符串中

print("eogn" in "hello eogn")
print("e" in "hello eogn")

True
True

判断元素是否存在于列表

print(1 in [1,2,3])
print('x' in ['x','y','z'])

True
True

判断key是否存在于列表

print('k1' in {'k1':111,'k2':222})
print(111 in {'k1':111,'k2':222})

True
False

2.not in

判断一个字符或者字符串是否存在于一个大字符串中

print("eogn" not in "hello eogn")
print("e" not in "hello eogn")

False
False

判断元素是否存在于列表

print(1 not in [1,2,3])
print('x' not in ['x','y','z'])

False
False

判断key是否存在于列表

print('k1' not in {'k1':111,'k2':222})
print(111 not in {'k1':111,'k2':222})

False
True

身份运算符

1.is

是判断两个标识符是不是引用自一个对象

如果引用的是同一个对象则返回 True,否则返回 False

x = 1
y = 1
print(x is y)

True
x = 1
y = 2
print(x is y)

False

2.not is

是判断两个标识符是不是引用自不同对象

如果引用的不是同一个对象则返回结果 True,否则返回 False

x = 1
y = 1
print(x is not y)

False
x = 1
y = 2
print(x is not y)

True

思维导图(点击链接

原文地址:https://www.cnblogs.com/zhww/p/12982137.html