Python Boolean
Python Booleans are used to represent the truth value of an expression.
The Python Boolean type has only two possible values: True
and False
.
Note that the boolean values True
and False
begin with the capital letters T
and F
.
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 = False
In general, a boolean value is obtained as the result of comparison operators or from functions that return booleans.
See paragraphs below.
Boolean 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) # False
Or 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) # False
Not Operation
The not
operator returns the inverse value to the input value.
print(not True) # False
print(not False) # True
Comparison 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) # False
Order 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'
Order comparison operators are not defined for all objects (also because some objects have no meaningful order like dictionaries).
Python does not require comparison operators to return booleans. Third-party libraries may return different values.
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) # True
The examples above show the is
operator used only with lists. The behavior of the is
operator on immutable objects such as numbers and strings is more complicated.
in
Operator
The in
operator checks for membership.
An object can define what it considers to be members.
Most sequences, such as lists, consider their elements to be members.
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) # False
Conversion 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({})) # False
The bool()
function will always return True, unless:
- The object is empty, like
[]
,()
,{}
,''
- The object is False
- The object is 0
- The object is None
Numbers 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))
All nonzero integers are true. This also applies to floating-point numbers, including special floating-point numbers such as infinity and Not a Number (NaN).
Other command numbers datatype:
fractions
(from thefractions
module) are False only when they are equal to0
.decimal
(from thedecimal
module) 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"")) # False
All Python built-in objects that have a length follow this rule.
So this rule is not guaranteed to work for non-built-in objects!
None
as a Boolean Value
None
is always evaluated as False.
print(bool(None)) # False
It is best to explicitly verify the identity with is None
.