如何在 Python 中获取布尔值的否定?


在本文中,我们将学习如何在 Python 中获取布尔值的否定。

在 Python 中,布尔数据类型是内置数据类型。它表示 **True** 或 **False** 值。例如,5<20 为 True,而 10>20 为 False。在本文中,我们将打印布尔变量的否定。

以下是完成此任务的各种方法:

  • 使用“~”运算符

  • 使用“not”运算符

  • 使用 Operator 模块

  • 从‘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()** 函数返回输入布尔数组值的否定值的列表:

# 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

结论

本文向我们介绍了 5 种在 Python 中获取布尔值否定的不同方法。我们还学习了如何使用 bool() 方法将任何结果(例如表达式或值)转换为布尔类型。

更新于: 2023年1月27日

6K+ 次浏览

开启您的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.