Python. Logic
In the last lesson I have left one particular data type unexplained, as it is distinctly different from all the other ones and kinda responsible for the representing logic in the programming language. All the values that belong to the boolean data type can be either True or False. Yes or No. Black or White. There is no middle ground, gray area or compromise. The language evaluates every expression in the program to be either True or False. But what do I mean by that? What is True? What is False? Do we need to study rhetoric in order to be able to program? That’s not what I had been thinking programming was about!!!

Relax. Fortunately the program will not demand from you being capable of answering philosophical questions regarding the meaning and purpose of human existence. What the language conceives as true is everything that corresponds with the common sense. 2 equals 1+1 is True. 5 is larger than 10 is False. Python has in addition to arithmetic operators which we have been discussing earlier, the concept of comparison operators, symbols that allow us to compare two expressions with each other, and then return depending on the condition, whether the comparison is True or False:
print ( 2 == 1+1) # is 2 equal to 1+1
print ( 5 > 10)
print ( 65 < 100–30)
print( “Rodger” == “Ben”)
Result:
True
False
True
False
See? Every time you make a comparison between two different expressions under a certain condition, such as this is equal to that, it will return a Boolean value that shows us the answer to the question which we had asked. Sometimes you might want to know whether the value of a particular variable in your program has a certain expected value. Maybe you want to make sure that the output of your function will never exceed 1000 or that the global variable, which has been changed multiple times throughout the running session of your script is still of the same data type. You can do that and much more with the comparison operators.
In fact you can even assign boolean values directly:
apple = True
orange = False
print apple
print orange
Result:
True
False
However given the information that you have right now, it seems absolutely pointless. Yeah, I kinda can have a lot of boolean values, assign them to variables and then watch how they are being printed out, but it is nothing exciting. What is the point of having some feature if you cannot use it in some practical way? Well, they do have a use as, you can combine boolean values to obtain…… another boolean value!!! Isn’t it awesome?

Boolean logic also includes operators such as NOT, OR, AND, NOT OR, NOT AND. They allow us to do all sorts of things with boolean values, as we can we make a giant sequence of comparisons resulting in complex expression that produces a single boolean value desired by us. Let’s actually look at the following examples to understand how these operators work, and why do we need them. Assume that we have a variable T that has a value True, and a variable F that has the value False:
T = True
F = False
print(T)
print(F)
Result:
True
False
That’s clear now, but what if we try to apply the operator NOT to both of these variables:
T = T
F = F
print(NOT T)
print(NOT F)
Result:
False
True
Not against intuition, the operator NOT just returns whatever boolean value that is opposite to the expression upon which it was used. We can see that, from even more plain examples:
print ( NOT (2 == 1+1)) # is 2 equal to 1+1
print (NOT 5 > 10)
print (NOT 65 < 100–30)
print(NOT “Rodger” == “Ben”)
Result:
False
True
False
True
This is quite easy to understand, but what the operator AND does? It evaluates two boolean expressions, and returns True only when both of these expressions are true, otherwise it will return False. Let’s take look right now:
T = True
F = False
print(T and T)
print(T and F)
print(F and T)
print(F and F)
Result:
True
False
False
False
Okay, it might be a little confusing, but when you look at this operator in action it clears up everything:
print(5> 2 and 1 == 1)
print(2 < 7 and 6 > 11)
print(58 == 21 and 54 >= 45)
print(54 == 1 and 0 > 65)
Result:
True
False
False
False
AND just simply cannot return True when one of the exrpessions is False, because if it did it would have been against all common sense.
The next one OR is quite simple. It returns True every time one of the expressions under examination is True, otherwise it will return False. Let’s take a look:
T = True
F = False
print(T or T)
print(T or F)
print(F or T)
print(F or F)
Result:
True
True
True
False
Again, when you look at this operator in actions it much much easier to understand how it works. Though I doubt you would have any problems with that:
print(5> 2 or 1 == 1)
print(2 < 7 or 6 > 11)
print(58 == 21 or 54 >= 45)
print(54 == 1 or 0 > 65)
Result:
True
True
True
False
If the operator AND was really strict and wanted everything to be true, then OR is much more compromising and would be okay, as long as only one of the expressions was True. The last two operators NOT AND and NOT OR might look really weird and incomprehensive at first, but they just the same AND and OR operators, except that on their results the operator NOT was applied.
To illustrate, everytime the operator AND will return True value, NOT AND will return False, and vica versa. The same goes for NOT OR so you should not have any trouble using them, just apply their ordinary forms first then convert it to the opposite value, and you will be okay:
T = True
F = False
print(not (T and T))
print(not (T and F))
print( not (F and T))
print(not (F and F))
Result:
False
True
True
True
T = True
F = False
print( not (T or T))
print( not (T or F))
print( not (F or T))
print( not (F or F))
Result:
False
False
False
True
You might be wondering to yourself, with greedy and dissatisfied countenance on your face, barely holding your avarice and coding hunger, but just two boolean values is not enough!!! I want more!!! I want to evaluate 9999 boolean values and see the result!

Well, you can. Nothing is stopping you. The same rules will still aplly, even when you increase the number of boolean values to the million.In fact the act of creating an extremely complex boolean expression is an art by itself, not to mention just how fun it is.
See you next time!!!!!
P.S. You can already go and play with booleans a bit. Let you imagination run wild.
