Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 a built-in data type that represents True or False values. For example, 5<20 is True and 10>20 is False. Boolean negation means converting True to False and False to True.
The following are the various methods to accomplish this task ?
Using the "not" operator
Using the "~" operator
Using operator module
Using arithmetic subtraction from 1
Using NumPy functions
Method 1: Using the "not" Operator
The not operator is the most common and pythonic way to negate a Boolean value. It returns the logical complement of the operand.
Example
The following program demonstrates Boolean negation using the not operator ?
# input boolean values
bool1 = True
bool2 = False
print("Original values:")
print("bool1 =", bool1)
print("bool2 =", bool2)
print("\nNegated values:")
print("not bool1 =", not bool1)
print("not bool2 =", not bool2)
# Works with expressions too
print("\nWith expressions:")
print("5 > 3 =", 5 > 3)
print("not (5 > 3) =", not (5 > 3))
Original values: bool1 = True bool2 = False Negated values: not bool1 = False not bool2 = True With expressions: 5 > 3 = True not (5 > 3) = False
Method 2: Using the "~" Operator
The bitwise NOT ("~") operator performs bitwise negation. When used with Boolean values, it requires conversion using bool() to get the expected result.
Example
The following program returns the negation using the bitwise NOT operator ?
# input boolean value
input_bool = False
print("Input boolean value:", input_bool)
# Using ~ operator (requires bool() conversion)
negated = bool(~input_bool)
print("Negation using ~ operator:", negated)
# Demonstrating with True value
input_bool2 = True
print("\nInput boolean value:", input_bool2)
print("Negation using ~ operator:", bool(~input_bool2))
Input boolean value: False Negation using ~ operator: True Input boolean value: True Negation using ~ operator: False
Method 3: Using Operator Module
The operator module provides the not_() function which performs logical negation equivalent to the not operator.
Example
The following program demonstrates negation using the operator module ?
import operator
# input values
input_bool = True
input_string = "tutorialspoint"
print("Boolean negation:")
print("Input:", input_bool)
print("Negated:", operator.not_(input_bool))
print("\nString negation (truthy to falsy):")
print("Input string:", input_string)
print("bool(input_string):", bool(input_string))
print("operator.not_(input_string):", operator.not_(input_string))
Boolean negation: Input: True Negated: False String negation (truthy to falsy): Input string: tutorialspoint bool(input_string): True operator.not_(input_string): False
Method 4: Using Arithmetic Subtraction from 1
For Boolean values (which are integers 0 and 1 in Python), subtracting from 1 effectively negates the value.
Example
The following program negates Boolean values using arithmetic subtraction ?
# input boolean values
input_bool1 = False
input_bool2 = True
print("Method: 1 - boolean_value")
print("Input:", input_bool1, "? Negated:", bool(1 - input_bool1))
print("Input:", input_bool2, "? Negated:", bool(1 - input_bool2))
# Showing the integer nature of booleans
print("\nBoolean as integers:")
print("False as int:", int(input_bool1))
print("True as int:", int(input_bool2))
Method: 1 - boolean_value Input: False ? Negated: True Input: True ? Negated: False Boolean as integers: False as int: 0 True as int: 1
Method 5: Using NumPy Functions
NumPy provides logical_not() and bitwise_not() functions for negating Boolean arrays and individual values.
Example
The following program demonstrates NumPy Boolean negation functions ?
import numpy as np
# Single boolean value
single_bool = False
print("Single value negation:")
print("Input:", single_bool)
print("logical_not():", np.logical_not(single_bool))
# Boolean array
bool_array = np.array([True, False, True, False])
print("\nArray negation:")
print("Input array:", bool_array)
print("logical_not():", np.logical_not(bool_array))
print("bitwise_not():", np.bitwise_not(bool_array))
Single value negation: Input: False logical_not(): True Array negation: Input array: [ True False True False] logical_not(): [False True False True] bitwise_not(): [False True False True]
Comparison
| Method | Syntax | Best For | Notes |
|---|---|---|---|
not |
not value |
General use | Most pythonic |
~ |
bool(~value) |
Bitwise operations | Requires bool() conversion |
operator.not_() |
operator.not_(value) |
Functional programming | Equivalent to not operator |
| Arithmetic | bool(1 - value) |
Mathematical context | Works due to bool/int relationship |
| NumPy | np.logical_not(value) |
Arrays and scientific computing | Best for array operations |
Conclusion
The not operator is the most readable and efficient method for Boolean negation in Python. Use NumPy functions for array operations and operator.not_() for functional programming contexts.
