- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Get a Negation of a Boolean in Python?
In this article, we will learn how to get a negation of a Boolean in Python.
In Python, the Boolean datatype is the built-in data type. It denotes the True or False values. For example, 5<20 is True and 10>20 is False. In this article, we will print the negation of a Boolean variable.
The following are the various methods to accomplish this task −
Using the “~” operator
Using “not” Operator
Using Operator Module
Using Minus the value from ‘1’
Using bitwise_not(), logical_not() of Numpy Module
Method 1: Using the “~” operator
The negation of the operand can be returned using the bitwise NOT(“~”) operator.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task. −
Create a variable to store the input boolean value.
Print the input boolean value.
Use the ~ operator to print the negation of the input boolean value and print the resultant value.
The bool() function will convert to a Boolean value if it is not already a boolean value before.
Example
The following program returns the negation of the input boolean value using the bitwise NOT(“~”) operator −
# 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))
Output
On executing, the above program will generate the following output −
Input boolean value: False Negation of the input boolean value: True
Method 2:Using “not” Operator
The not operator is a logical operator that returns the complement(negation) of the operand's Boolean value.
Use the not operator to print the negation of the input boolean value and print the resultant value.
Example
The following program returns the negation of the input boolean value using the “not” Operator −
# 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)
Output
On executing, the above program will generate the following output −
Input boolean value: False Negation of the input boolean value: True
In the above example, we are using print(bool(inputBool)), as "inputBool" will be changed to a Boolean value if it is not already a boolean value before.
Example
The following program removes the last element from the set using the remove() function −
# 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)
Output
On executing, the above program will generate the following output −
Input boolean value: True Negation of the input string: False
Method 3: Using Operator Module
Use the following code to import the Operator module before running the program −
import operator
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task. −
Use the import keyword to import the operator module.
Use the bool() function to convert the input string into a boolean datatype and print a boolean value.
Use the operator.not_() function to print the negation of the boolean value and print the resultant value.
Example
The following program returns the negation of the input boolean value using the operator.not_() function −
# 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))
Output
On executing, the above program will generate the following output −
Input boolean value: True Negation of the input string: False
Method 4: Using Minus the value from ‘1’
Example
The following program returns the negation of the input boolean value using the function by subtracting the value from ‘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)
Output
Input boolean value: False Output boolean value: True
Method 5: Using bitwise_not(), logical_not() of Numpy Module
bitwise_not() function − The bitwise_not() function of the NumPy module returns the negation of the given Boolean argument.
Example
The following program returns a list of negation values of the input boolean array values using the bitwise_not() function of the NumPy module −
# 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))
Output
Input List: [False, True, True, False, False] Output List: [True, False, False, True, True]
Using numpy. logical_not() function
We can alternatively utilize the Numpy library's logical_not() method, which gives a Boolean value.
Example
The following program returns a list of negation values of the input boolean array values using the logical_not() function of the NumPy module −
# 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)
Output
On executing, the above program will generate the following output −
Input boolean value: False Output boolean value: True
Conclusion
This article taught us 5 distinct methods for getting a boolean's negation in Python. We also learned how to convert any result, such as an expression or value, to a boolean type using the bool() method.