Python 如何获得布尔值的否定
在本文中,我们将学习如何在Python中获得布尔值的否定。
在Python中,布尔数据类型是内置数据类型。它表示 True 或 False 的值。例如,5<20是True,10>20是False。在本文中,我们将打印布尔变量的否定。
以下是完成此任务的各种方法:
- 使用“~”运算符
-
使用“not”运算符
-
使用运算符模块
-
从 ‘1’ 减去值
-
使用Numpy模块的bitwise_not()和logical_not()
方法1:使用“~”运算符
可以使用位运算符 ~ 返回操作数的否定。
步骤
以下是执行所需任务的算法/步骤。
- 创建一个变量来存储输入的布尔值。
-
打印输入的布尔值。
-
使用 ~运算符 打印输入布尔值的否定并打印结果值。
-
在转换为布尔值之前, bool() 函数将先将其转换为布尔值。
示例
以下程序使用位运算符 ~ 返回输入布尔值的否定。
# input boolean value
inputBool = False
# printing the input boolean value
print("Input boolean value:", inputBool)
# Printing the negation of the input boolean value using the ~ operator
print("Negation of the input boolean value:", bool(~inputBool))
输出
在执行时,上述程序将生成以下输出−
Input boolean value: False
Negation of the input boolean value: True
方法2:使用“not”运算符
not运算符 是一个逻辑运算符,它返回操作数的布尔值的补集(否定)。
使用 not运算符 来打印输入布尔值的否定,并打印结果值。
示例
以下程序使用 “not” 运算符返回输入布尔值的否定 –
# input boolean value
inputBool = False
# printing the input boolean value
print("Input boolean value:", bool(inputBool))
# Printing the negation of the input boolean
# value using 'not' operator
print("Negation of the input boolean value:", not inputBool)
输出
执行以上程序后,将生成以下输出结果-
Input boolean value: False
Negation of the input boolean value: True
在上面的示例中,我们使用 print(bool(inputBool)) ,因为 “inputBool” 如果在之前不是布尔值,则会被转换为布尔值。
示例
以下程序使用remove()函数从集合中移除最后一个元素。
# input string
inputStr = "tutorialspoint"
# converting the input string into a boolean datatype
# using bool() function and printing a boolean value
print("Input boolean value:", bool(inputStr))
# Printing the negation of the boolean
# value using 'not' operator
print("Negation of the input string:", not inputStr)
输出
执行上述程序后,将生成以下输出 –
Input boolean value: True
Negation of the input string: False
方法3:使用Operator模块
在运行程序之前,使用以下代码导入Operator模块−
import operator
步骤
以下是执行所需任务的算法/步骤 –
- 使用import关键字导入operator模块。
-
使用bool()函数将输入的字符串转换为布尔类型并打印布尔值。
-
使用operator.not_()函数打印布尔值的否定并打印结果值。
示例
以下程序使用operator.not_()函数返回输入布尔值的否定 –
# importing operator module
import operator
# input string
inputStr = "tutorialspoint"
# converting the input string into a boolean datatype
# using bool() function and printing a boolean value
print("Input boolean value:", bool(inputStr))
# Printing the negation of the boolean
# value using the operator.not_() function
print("Negation of the input string:", operator.not_(inputStr))
输出
执行上述程序后,将生成以下输出:
Input boolean value: True
Negation of the input string: False
方法4:使用减去值‘1’
示例
以下程序通过将值从‘1’中减去来返回输入布尔值的否定。
# input boolean value
inputBool = False
# printing the input boolean value
print("Input boolean value:", inputBool)
# getting the negation of the input boolean value
# by subtracting it from 1 and converting it to a boolean value
outputBool = bool(1-inputBool)
# printing the resultant boolean value
print("Output boolean value:", outputBool)
输出
Input boolean value: False
Output boolean value: True
方法5:使用Numpy模块的bitwise_not()和logical_not()函数
bitwise_not()函数 − Numpy模块的bitwise_not()函数返回给定布尔参数的否定值。
示例
以下程序使用Numpy模块的bitwise_not()函数返回输入布尔数组值的否定列表值 − bitwise_not() 函数。
# importing NumPy module with an alias name
import numpy as np
# input NumPy array containing boolean elements
inputArray = np.array([False, True, True, False, False])
# converting input array to list and printing it
print("Input List:", list(inputArray))
# getting the negation values of the input array
# using the bitwise_not() function of NumPy module
outputArray = np.bitwise_not(inputArray)
# converting output array to list and printing it
print("Output List:", list(outputArray))
输出
Input List: [False, True, True, False, False]
Output List: [True, False, False, True, True]
使用numpy.logical_not()函数
我们可以选择使用Numpy库的logical_not()方法来得到一个布尔值。
示例
以下程序使用NumPy模块的logical_not()函数来返回输入布尔数组值的否定值列表。
# input boolean value
inputBool = False
# printing the input boolean value
print("Input boolean value:", inputBool)
# getting the negation of the input boolean value using logical_not() function
outputBool = np.logical_not(inputBool)
# printing the resultant boolean value
print("Output boolean value:", outputBool)
输出
执行时,上面的程序将生成以下输出 –
Input boolean value: False
Output boolean value: True
结论
本文教我们在Python中获取布尔值的5种不同方法。我们还学会了如何使用bool()方法将任何结果,如表达式或值,转换为布尔类型。