Python Booleans are used to represent the truth value of an expression.
The Python Boolean type has only two possible values: True and False.
Create Python Boolean
It is sufficient to assign to a variable a value or an expression that returns a boolean value.
my_boolean = True
is_ok = FalseBoolean Operation
And Operation
The operator and returns True if all operands are True, otherwise False.
print(True and True) # True
print(True and False) # False
print(False and True) # False
print(False and False) # FalseOr Operation
The or operator returns True if at least one of the elements is True, otherwise False.
print(True or True) # True
print(True or False) # True
print(False or True) # True
print(False or False) # FalseNot Operation
The not operator returns the inverse value to the input value.
print(not True) # False
print(not False) # TrueComparison Operators
Comparison operators always return Boolean values.
Equality and Inequality
The most common comparison operators are the **equality operator == and the inequality operator !=.
print(1 == 1) # True
print(1 == 1.0) # True
print(1 == 2) # False
print(1 != 2) # True
print(1 != 1) # FalseOrder Comparisons
Order comparison operators are < , <=, > and >=.
print(1 <= 1)
print(1 < 1)
print(2 > 3)
print(2 >= 2)In addition, type comparisons are not supported
print(1 <= "1")Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<=' not supported between instances of 'int' and 'str'Chaining Comparison Operator
is Operator
The is operator checks for objects identity, i.e., x is y evaluates True only when x and y evaluate the same object.
x = []
y = []
print(x is x) # True
print(x is not x) # False
print(x is y) # False
print(x is not y) # Truein Operator
The in operator checks for membership.
For example:
my_list = [2, 4, 6, 8, 10]
print(1 in my_list) # False
print(2 in my_list) # True
print(10 in my_list) # True
print(10 not in my_list) # FalseConversion other datatypes in Boolean
The bool() function
The bool() function returns the boolean value of a specified object.
print(bool('Hi')) # True
print(bool(100)) # True
print(bool(0)) # False
print(bool(1)) # True
print(bool('')) # False
print(bool(())) # False
print(bool([])) # False
print(bool({})) # FalseNumbers as Boolean Values
For numbers, bool(x) is equivalent to x != 0.
This means that it returns False only with 0.
print(bool(3))
print(bool(-5))
print(bool(0))Other command numbers datatype:
fractions(from thefractionsmodule) are False only when they are equal to0.decimal(from thedecimalmodule) are False only when they are equal to0.
Sequences as Boolean Values
In general, objects that have a len() are False when the result of len() is 0 (no matter whether they are lists, tuples, sets, strings, or byte strings)
print(bool([1])) # True
print(bool([])) # False
print(bool((1,2))) # True
print(bool(())) # False
print(bool({1,2,3})) # True
print(bool(set()))) # False
print(bool({1: 2})) # True
print(bool({})) # False
print(bool("TutRef")) # True
print(bool("")) # False
print(bool(b"xyz")) # True
print(bool(b"")) # FalseNone as a Boolean Value
None is always evaluated as False.
print(bool(None)) # False