Skip to content

Python Boolean

Understand Python Booleans with clear examples of True/False values, logical operations (and, or, not), comparison operators (==, !=, <, >, is, in), and type conversion rules using bool().

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 = False

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'

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

in 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) # 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

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))

Other command numbers datatype:

  • fractions (from the fractions module) are False only when they are equal to 0.
  • decimal (from the decimal module) are False only when they are equal to 0.

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

None as a Boolean Value

None is always evaluated as False.

print(bool(None)) # False
Navigation