2.1.4 - Boolean variables

Python supports Boolean variables. What are Boolean variables? Boolean variables can store either a True or a value of False. Boolean algebra was developed by George Boole back in 1854. If only he knew how important his work would become as the basis for modern computer logic!

An if statement needs an expression to evaluate to True or False. What may seem odd is that it does not actually need to do any comparisons if a variable already evaluates to True or False.

# Boolean data type. This is legal! 
a = True
if a:
    print("a is true")

Ok so a is (==) True. And what about not a. Is it also True? Of course not. If a is True then not a should be False and if a is False then not a should be True

How can we write this using code? It is very simple.

The following two examples are both correct.

# How to use the not function - example 1 a = False if not(a): # What is not(a) ? print("a is false") # How to use the not function - example 2 if not a: # What is not a ? print("a is false")


Of course we can combine statements using and

a = True b = True if a and b: print("a and b are both true") 


or or operands 

  a = True b = False if a or b: print("one or both of a and b are true")