Boolean Variables, Logical Operators & Truth Tables In Python

Hi there. This page is a short guide to boolean variables and truth tables in Python.

Reference: Starting Out With Python – Second Edition By Tony Gaddis

 


Boolean Variables

A boolean variable is a variable that can either take on True or False. Boolean variables can be viewed as signals or flags which can help with if statements.

 

Instead of passed == True:, you may see just if passed: which means the same thing.

 


Logical Operators & Truth Tables

Before getting into truth tables, it is important to to introduce the logical operators and, or.

 

or Operator

This or operator connects two conditions together into one. For the or statement to be true, one of the two or both of them have to be true.

  1. True or True = True
  2. True or False = True
  3. False or True = True
  4. False or False = False

 

and Operator

With the and operator, both statements have to be true in for the and compound expression to be true.

  1. True and True = True
  2. True and False = False
  3. False and True = False
  4. False and False = False

 

not Operator

Like in the English language, not means the opposite. There are no gray areas (half-truths, half lies and the like).

  1. not True = False
  2. not False = True

 

Leave a Reply