如何在 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.