How to Get a Negation of a Boolean in Python
Last Updated :
27 Jul, 2023
Boolean datatype is the built-in data type in Python. It represents the True or False values. Like 0<1 is True and 7>10 is False. The task is to print the negation of a Boolean variable
Input: True
Output: False
Input: False
Output: True
Input: "GeeksforGeeks"
Output: False
Input: [True, True, False, True, False]
Output: [False, False, True, False, True]
To implement this task, Python has many built-in operators and functions. Let’s explore them one by one.
How to Get a Negation of a Boolean in Python Using the “~” operator
This is the Bitwise NOT operator which can be used to return the negation of the operand.
Python3
a = False
print (a)
print ( bool (~a))
|
Output :
False
True
Time Complexity: O(1)
Auxiliary Space: O(1)
Negation of a Boolean in Python Using “not” Operator
Not operator is the logical operator which returns the complementary of the Boolean value of the operand.
Python3
a = False
print ( bool (a))
print ( not a)
|
Output :
False
True
Time Complexity: O(1)
Auxiliary Space: O(1)
Here, we are using print(bool(a)), as if the “a” is not a Boolean value then it will be converted into it.
Let’s understand this with another example :
Python3
a = "GeeksforGeeks"
print ( bool (a))
print ( not a)
|
Output :
True
False
Time Complexity: O(1)
Auxiliary Space: O(1)
Get a Negation of a Boolean in Python Using Operator Module
Before implementing the code, import the Operator module using below code.
import operator
This method uses the operator.not_() function to return the negation of the Boolean value. It has a constant time complexity and requires constant auxiliary space.
Python3
a = "GeeksforGeeks"
print ( bool (a))
print (operator.not_(a))
|
Output:
True
False
Time Complexity: O(1)
Auxiliary Space: O(1)
We can also implement the negation in list elements using operator.not_() function
Python3
input_list = [ True , True , False , True , False ]
print ( list (input_list))
output_list = map (operator.not_, input_list)
print ( list (output_list))
|
Output :
[True, True, False, True, False]
[False, False, True, False, True]
Note: To negate the values in the python list, always use the above syntax. As operator.not_(input_list) will consider the whole list as an input and perform accordingly.
Time Complexity: O(n)
Auxiliary Space: O(n)
Get a Negation of a Boolean in Python Using Numpy Module
Before implementing the code, import the Numpy library using below code.
import numpy as np
This method uses the numpy library and has two functions: bitwise_not() and logical_not().
1. bitwise_not() function returns the negation value of the given Boolean argument.
Python3
b = np.array([ True , True , False , True , False ])
print ( list (b))
b = np.bitwise_not(b)
print ( list (b))
|
Output :
[True, True, False, True, False]
[False, False, True, False, True]
Time Complexity: O(n)
Auxiliary Space: O(n)
2. We can also use logical_not() function, it is also the function of Numpy library and returns the Boolean value.
Python3
b = True
print (b)
b = np.logical_not(b)
print (b)
|
Output :
True
False
Time Complexity: O(1)
Auxiliary Space: O(1)
Get a Negation of a Boolean in Python Minus the value with ‘1’:
This method subtracts the boolean data-type value to 1 and typecasts it back to boolean to get the negation of the original boolean value. It has a constant time complexity and requires constant auxiliary space.
Python
b = True
print (b)
b = bool ( 1 - b)
print (b)
|
Output:
True
False
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Python | Boolean List AND and OR operations
Sometimes, while working with Python list, we can have a problem in which we have a Boolean list and we need to find Boolean AND or OR of all elements in it. This kind of problem has application in Data Science domain. Let's discuss an easy way to solve both these tasks. Method #1 : AND operation -
3 min read
How to Get Index of a Substring in Python?
To get index of a substring within a Python string can be done using several methods such as str.find(), str.index(), and even regular expressions. Each approach has its own use case depending on the requirements. Letâs explore how to efficiently get the index of a substring. The simplest way to get
2 min read
Python | Count true booleans in a list
Given a list of booleans, write a Python program to find the count of true booleans in the given list. Examples: Input : [True, False, True, True, False] Output : 3 Input : [False, True, False, True] Output : 2 Method #1: Using List comprehension One simple method to count True booleans in a list is
3 min read
Python - False indices in a boolean list
Boolean lists are often used by the developers to check for False values during hashing. These have many applications in developers daily life. Boolean list is also used in certain dynamic programming paradigms in dynamic programming. Also in Machine Learning preprocessing of values. Lets discuss ce
7 min read
Python - Test Boolean Value of Dictionary
Sometimes, while working with data, we have a problem in which we need to accept or reject a dictionary on the basis of its true value, i.e all the keys are Boolean true or not. This kind of problem has possible applications in data preprocessing domains. Let's discuss certain ways in which this tas
9 min read
Python - Negative index of Element in List
We are given a list we need to find the negative index of that element. For example, we are having a list li = [10, 20, 30, 40, 50] and the given element is 30 we need to fin the negative index of it so that given output should be -3. Using index()index() method in Python searches for the first occu
3 min read
Boolean list initialization - Python
We are given a task to initialize a list of boolean values in Python. A boolean list contains elements that are either True or False. Let's explore several ways to initialize a boolean list in Python. Using List MultiplicationThe most efficient way to initialize a boolean list with identical values
3 min read
How to check NoneType in Python
The NoneType object is a special type in Python that represents the absence of a value. In other words, NoneType is the type for the None object, which is an object that contains no value or defines a null value. It is used to indicate that a variable or expression does not have a value or has an un
2 min read
Python program to fetch the indices of true values in a Boolean list
Given a list of only boolean values, write a Python program to fetch all the indices with True values from given list. Let's see certain ways to do this task. Method #1: Using itertools [Pythonic way] itertools.compress() function checks for all the elements in list and returns the list of indices w
5 min read
How to Convert Bool (True/False) to a String in Python?
In this article, we will explore various methods for converting Boolean values (True/False) to strings in Python. Understanding how to convert between data types is crucial in programming, and Python provides several commonly used techniques for this specific conversion. Properly handling Boolean-to
2 min read