Using Booleans#

One of the built-in types beside string and numbers in Python is the Boolean data type and also follows Boolean algebra (see Boolean algebra).

The basics about booleans#

A boolean is a value of either True or False. This value can be assigned to a variable or the result of an expression. In the following example the expressions are evaluated by Python on lines 4 to 6 and the result is printed. On lines 7 to 9 the result of the value is evaluated by bool and printed. Python sees the value 0 as False and all other values as True.

Example of diffent logical operators#
 1#!/usr/bin/env python3
 2
 3def main():
 4    print(1 > 2)
 5    print(1 == 2)
 6    print(1 < 2)
 7    print(bool(0))
 8    print(bool(1))
 9    print(bool(2))
10
11
12if __name__ == "__main__":
13    main()
Output#
1False
2False
3True
4False
5True
6True

The most common use of booleans is to check conditions with Comparison operators. The following example checks with the great-than operator if an expression is True or False and prints the result of the branch of the check condition.

Example of logical operator >#
 1#!/usr/bin/env python3
 2
 3def main():
 4    x = 10
 5    y = 5
 6
 7    if x > y:
 8        print("x is greater than y")
 9    else:
10        print("x is not greater than y")
11
12
13if __name__ == "__main__":
14    main()

The output of the example is x is greater than y as 10 is greater than 5.

Output#
1x is greater than y

Some values are false#

Determining if a value is True or False with bool is based on the value. If the value is zero then this is considered as False and if the value is not zero it is assumed to be True.

Example#
1#!/usr/bin/env python3
2
3def main():
4    print(bool(0))
5    print(bool(1))
6    print(bool(2))
7
8if __name__ == "__main__":
9    main()
Output#
1False
2True
3True

Note

Casting integer values from functions like len() with bool enables the option to act on them as if they were returning a boolean result as show in the example below.

Example of using bool with len()#
1if bool(len(value)):
2    print("String has one or more characters")
3else:
4    print("String is empty")

Functions can return a boolean#

As seen in the previous section Some values are false that integer values from functions can be casted to a boolean value. In chapter Functions the complete working of functions is explained, but in the example below a function is defined on lines 3 and 4 that always return True. This way the condition can directly be evaluated.

Example#
 1#!/usr/bin/env python3
 2
 3def trueFunction():
 4    return True
 5
 6
 7def main():
 8    if trueFunction():
 9        print("True")
10    else:
11        print("False")
12
13
14if __name__ == "__main__":
15    main()
Output#
1True

Boolean operators basics#

As seen in the previous sections on how comparisons return a boolean state, or how integer values can be casted to a boolean value the next step is to combine them with Logical operators. This to simplify the control flow inside the application and make it more readable and understandable.

The rudimentary symmetric Boolean functions are:

  • NOT, negation or complement - which receives one input and returns true when that input is false (“not”)

  • AND or conjunction - true when all inputs are true (“both”)

  • OR or disjunction - true when any input is true (“either”)

  • XOR or exclusive disjunction - true when one of its inputs is true and the other is false (“not equal”)

Constructed Boolean operators:

  • NAND or Sheffer stroke - true when it is not the case that all inputs are true (“not both”)

  • NOR or logical nor - true when none of the inputs are true (“neither”)

  • XNOR or logical equality - true when both inputs are the same (“equal”)

The NOT operator#

If value A is NOT true, then Result is true

Applying the rules for NOT results in the table below where value A is inverted into the Result.

Value A

Result

False

True

True

False

In the example below on line 6 the variable value is inverted with the not keyword.

Example of an NOT comparison#
 1#!/usr/bin/env python3
 2
 3def main():
 4    value = True
 5
 6    if not value:
 7        print("True")
 8    else:
 9        print("False")
10
11
12if __name__ == "__main__":
13    main()

The AND operator#

If both values A and B are true, then Result is true

Applying the rules for AND results in the table below where Result only True if all input values are True.

Value A

Value B

Result

False

False

False

False

True

False

True

False

False

True

True

True

In the example below on line 7 both variables are evaluated with the and keyword.

Example of an AND comparison#
 1#!/usr/bin/env python3
 2
 3def main():
 4    value_a = True
 5    value_b = False
 6
 7    if value_a and value_b:
 8        print("True")
 9    else:
10        print("False")
11
12
13if __name__ == "__main__":
14    main()
Output#
1False

The OR operator#

If either values A or B is true, then Result is true

Applying the rules for OR results in the table below where Result only True if any input values is True.

Value A

Value B

Result

False

False

False

False

True

True

True

False

True

True

True

True

In the example below on line 7 both variables are evaluated with the or keyword.

Example of an OR comparison#
 1#!/usr/bin/env python3
 2
 3def main():
 4    value_a = True
 5    value_b = False
 6
 7    if value_a or value_b:
 8        print("True")
 9    else:
10        print("False")
11
12
13if __name__ == "__main__":
14    main()
Output#
1True

The XOR operator#

If only value A or B is true, then Result is true

Applying the rules for XOR results in the table below where Result only True if only one input values a True.

Value A

Value B

Result

False

False

False

False

True

True

True

False

True

True

True

False

In the example below on line 7 both variables are evaluated with the xor keyword.

Example of an XOR comparison#
 1#!/usr/bin/env python3
 2
 3def main():
 4    value_a = True
 5    value_b = False
 6
 7    if value_a xor value_b:
 8        print("True")
 9    else:
10        print("False")
11
12
13if __name__ == "__main__":
14    main()
Output#
1True

The NAND operator#

If both values A and B aren’t true, then Result is true

Applying the rules for NAND results in the table below where Result only True if all input values are not True.

Value A

Value B

Result

False

False

True

False

True

True

True

False

True

True

True

False

In the example below on line 7 both variables are evaluated with the and keyword and that result is inverted with not.

Example of an NAND comparison#
 1#!/usr/bin/env python3
 2
 3def main():
 4    value_a = True
 5    value_b = False
 6
 7    if not (value_a and value_b):
 8        print("True")
 9    else:
10        print("False")
11
12
13if __name__ == "__main__":
14    main()
Output#
1True

The NOR operator#

If both values A and B are False, then Result is true

Applying the rules for NOR results in the table below where Result only True if all input values are False.

Value A

Value B

Result

False

False

True

False

True

False

True

False

False

True

True

False

In the example below on line 7 both variables are evaluated with the or keyword and that result is inverted with not.

Example of an NOR comparison#
 1#!/usr/bin/env python3
 2
 3def main():
 4    value_a = True
 5    value_b = False
 6
 7    if not (value_a or value_b):
 8        print("True")
 9    else:
10        print("False")
11
12
13if __name__ == "__main__":
14    main()
Output#
1False

The XNOR operator#

If values A and B are equel, then Result is true

Applying the rules for XNOR results in the table below where Result only True if all input values are True or if all input values are False.

Value A

Value B

Result

False

False

True

False

True

False

True

False

False

True

True

True

In the example below on line 7 both variables are evaluated with the xor keyword and that result is inverted with not.

Example of an XNOR comparison#
 1#!/usr/bin/env python3
 2
 3def main():
 4    value_a = True
 5    value_b = False
 6
 7    if not (value_a xor value_b):
 8        print("True")
 9    else:
10        print("False")
11
12
13if __name__ == "__main__":
14    main()
Output#
1False