Booleans

One of the built-in data types 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:

 1 #!/usr/bin/env python3
 2
 3 def 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
12 if __name__ == "__main__":
13     main()

Output:

1 False
2 False
3 True
4 False
5 True
6 True

The most common use of booleans is to check conditions. The following example checks if an expression is True or False and prints the result of the branch of the check condition.

Example:

 1 #!/usr/bin/env python3
 2
 3 def 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
13 if __name__ == "__main__":
14     main()

Output:

1 x is greater than y

Some values are false

Example:

 1 #!/usr/bin/env python3
 2
 3 class myClass():
 4     def __len__(self):
 5         return 0
 6
 7
 8 def main():
 9     myobj = myClass()
10     print(bool(myobj))
11
12
13 if __name__ == "__main__":
14     main()

Output:

1 False

Functions can also return a boolean

Example:

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

Output:

1 True

Boolean operators basics

The rudimentary symmetric Boolean functions (logical connectives or logic gates) 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

Note

If A is NOT true, then Q is true

Value A

Result

0

1

1

0

Example:

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

The AND operator

Note

If both A and B are true, then Q is true

Value A

Value B

Result

0

0

0

0

1

0

1

0

0

1

1

1

Example:

 1 #!/usr/bin/env python3
 2
 3 def 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
13 if __name__ == "__main__":
14     main()

Output:

1 False

The OR operator

Note

If either A or B is true, then Q is true

Value A

Value B

Result

0

0

0

0

1

1

1

0

1

1

1

1

Example:

 1 #!/usr/bin/env python3
 2
 3 def 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
13 if __name__ == "__main__":
14     main()

Output:

1 True

The XOR operator

Value A

Value B

Result

0

0

0

0

1

1

1

0

1

1

1

0

Example:

 1 #!/usr/bin/env python3
 2
 3 def 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
13 if __name__ == "__main__":
14     main()

Output:

1 True

The NAND operator

Value A

Value B

Result

0

0

1

0

1

1

1

0

1

1

1

0

Example:

 1 #!/usr/bin/env python3
 2
 3 def 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
13 if __name__ == "__main__":
14     main()

Output:

1 True

The NOR operator

Value A

Value B

Result

0

0

1

0

1

0

1

0

0

1

1

0

Example:

 1 #!/usr/bin/env python3
 2
 3 def 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
13 if __name__ == "__main__":
14     main()

Output:

1 False

The XNOR operator

Value A

Value B

Result

0

0

1

0

1

0

1

0

0

1

1

1

Example:

 1 #!/usr/bin/env python3
 2
 3 def 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
13 if __name__ == "__main__":
14     main()

Output:

1 False