Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding logic in python (Exercise 27 of Learn Python the Hard Way)

Tags:

python

logic

Could someone explain exercise 27 of learn python the hard way to me?

Here is an image showing the section i don't understand. alt text

Is it saying that assuming the left column of the table is true, is the answer true?

eg if x = y is not false, is x=y true? yes.

but then.. if x = y is false and true, is x=y true? no??

like image 999
tobeannounced Avatar asked Nov 30 '22 17:11

tobeannounced


2 Answers

For each table, if you enter (expression in left-hand column) the result is (corresponding value in right-hand column); the table header shows the operator being explained. So, for example, if you enter "not True" as an expression, the result will be False.

Edit:

Geeze Louise - look, it's really simple. For example, the AND operator table:

If a is True  and b is True,  then a AND b is True
If a is True  and b is False, then a AND b is False
If a is False and b is True,  then a AND b is False
If a is False and b is False, then a AND b is False

This is all the table is trying to tell you - this is how the AND operator works. It takes two true-or-false values and returns a result based on how they combine. The table lists every possible combination of inputs and the result for each - it completely describes everything the operator can do. That's ALL it's trying to say.

Similarly for the other operators:

NOT is "the opposite of": if a is False, then Not a is True
AND is true if "both of" a and b are true
OR is true if "at least one of" a or b is true
== is "equal to", true if a and b have the same value
!= is "not equal to", true if a and b have different values

etc.

Edit2:

It might be easier if you think of some of these operators as electrical circuits (indeed, this analogy is precisely how your computer is built!).

AND is a sequential circuit - power on a wire running to switch A to switch B to a lightbulb. The lightbulb is on exactly and only when switch A is on and switch B is also on.

OR is a parallel circuit - the wire runs to switch A to the lighbulb, and also to switch B to the lightbulb. If either switch is on (or if both are on), the lightbulb is on.

== is a pair of two-way switches - the light is on only if both switches are up or if both switches are down.

Does that help?

like image 81
Hugh Bothwell Avatar answered Dec 05 '22 02:12

Hugh Bothwell


It's a truth table: the operations described (or, and, ==) can all be considered as applying just to True and False. In that case, to describe the operator completely you merely need to list all the possible inputs.

So, for instance, the operator or is defined as :

(True or True) is True
(True or False) is True
(False or True) is True
(False or False) is False

That completely explains what or does to boolean values.


If you're interested, that wiki page actually lists all the possible boolean binary operators:

0. Opq, false, Contradiction
1. Xpq, NOR, Logical NOR
2. Mpq, Converse nonimplication
3. Fpq, ¬p, Negation
4. Lpq, Material nonimplication
5. Gpq, ¬q, Negation
6. Jpq, XOR, Exclusive disjunction
7. Dpq, NAND, Logical NAND
8. Kpq, AND, Logical conjunction
9. Epq, XNOR, If and only if, Logical biconditional
10. Hpq, q, Projection function
11. Cpq, if/then, Logical implication
12. Ipq, p, Projection function
13. Bpq, then/if, Converse implication
14. Apq, OR, Logical disjunction
15. Vpq, true, Tautology
like image 23
Katriel Avatar answered Dec 05 '22 01:12

Katriel