python布尔运算符
In python, not is used for Logical NOT operator, and ~ is used for Bitwise NOT. Here, we will see their usages and implementation in Python.
在python中, not用于逻辑NOT运算符,而〜用于按位NOT。 在这里,我们将看到它们在Python中的用法和实现。
Logical NOT (not) operator is used to reverse the result, it returns "False" if the result is "True"; "True", otherwise.
逻辑NOT(非)运算符用于取反结果,如果结果为“ True”,则返回“ False”。 否则为“ True”。
Bitwise NOT (~) operator is used to invert all the bits i.e. it returns the one's complement of the number.
按位NOT(〜)运算符用于将所有位取反,即它返回一个数字的补码。
Python program of Logical NOT (not) operator
逻辑NOT(非)运算符的Python程序
# Logical NOT (not) operator
x = True
y = False
# printing the values
print("x: ", x)
print("y: ", y)
# 'not' operations
print("not x: ", not x)
print("not y: ", not y)
Output:
输出:
x: True
y: False
not x: False
not y: True
Python program of Bitwise NOT (~) operator
按位NOT(〜)运算符的Python程序
# Bitwise NOT (~) operator
x = True
y = False
# printing the values
print("x: ", x)
print("y: ", y)
# '~' operations
print("~ x: ", ~ x)
print("~ y: ", ~ y)
# assigning numbers
x = 123
y = 128
# printing the values
print("x: ", x)
print("y: ", y)
# '~' operations
print("~ x: ", ~ x)
print("~ y: ", ~ y)
Output:
输出:
x: True
y: False
~ x: -2
~ y: -1
x: 123
y: 128
~ x: -124
~ y: -129
翻译自: https://www.includehelp.com/python/logical-and-bitwise-not-operators-on-boolean.aspx
python布尔运算符