# Boolean truth tables

I don’t understand what this boolean operator truth tables is trying to show. Could someone explain this please?

Essentially when you’re working with `if` statements in any capacity, you was to get a boolean for the program to know if the code block should be run or not. For example `if True` would tell the if statement to execute the block, and `if False` would say to skip it. This also applies to any statements that would evaluate to `True` or `False`, for example `5 > 3` would give True. This would have been covered in the previous lessons.

However another thing we can do is combine statements to produce a result, and these are done using the “AND”, “OR” and “NOT” logical operators. In python these are represented simply by `and`, `or` and `not` (except when checking if not equal you use `!=` instead). What these operators do is take an input, and return a boolean value based on the evaluation of the code. So for example `5 > 3 and 7 < 3` would return `False`, as `and` requires both statements to be true for it to return `True`.

Therefore coming to the truth tables above, these are a way of showing all possible results for these operators. So the first row in AND represents taking the inputs `True and True`, which would return `True` since both sides of the `and` are `True`. Whilst the third line of OR represents taking the inputs `False or True`, which would return `True` since `or` only requires one side of the `or` statement to be `True`. By using these tables we can visualise how conditionals would evaluate and plan ahead our work.

An example, lets say we want to print “Dinner time” to the command line only if we are hungry and it’s getting late. Well then we know that in a logical sense, we only want to execute code if you are hungry and if it’s late enough to eat. Therefore logically we only want to execute code where input A will be `True` and input B will be `True`, and never any other time. Well from the tables we see that the only way to do that is using the “AND” operator, so we can use `and` to do this.

is_hungry = True; time_24h = 19; if is_hungry and time_24h > 18: print("Dinner Time!") else: print("Not yet!")

We can see that in it’s current state, it will print “Dinner Time!” to the console since it’s after 6pm and is_hungry is True. However if we break either of the conditions it will change to print “Not yet!”.

## TL;DR

Of course it’s fair to say that this is essentially the point of conditionals, however when starting out remembering exactly what you need for every type of condition can be tricky, and so these tables are meant to be a way to help you remember how the inputs and outputs work. You can see at a glance that `and` is only `True` if BOTH inputs are `True`, `or` is `True` if EITHER input is `True`, and `not` essentially just reverses the input. It can be a bit more abstract to see this in code, so hopefully the tables can help you track this in your programs and remind you of the right one for each scenario!

2 Likes

You could also just shortcut it (the diagram) and say that any `and` comparison that has `False` in it, is `False` and then any `or` comparison that has a `True` in it, is `True`. `not` negates both. Try it out in your terminal (in Python).

``````>>> True and True
True
>>> False and True
False
>>> not(True and False)
True
>>> False or True
True
>>> False or False
False
>>> not(False or True)
False
>>> not(not(True or True))
True
>>>
etc
``````
1 Like