The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Realization

--

Chapters
00:00 Understanding Logic In Python (Exercise 27 Of Learn Python The Hard Way)
00:36 Accepted Answer Score 6
02:20 Answer 2 Score 5
03:01 Answer 3 Score 3
03:25 Answer 4 Score 2
04:32 Thank you

--

Full question
https://stackoverflow.com/questions/4445...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #logic

#avk47



ACCEPTED ANSWER

Score 6


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?




ANSWER 2

Score 5


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



ANSWER 3

Score 3


I find the "True?" heading a bit misleading. It should be "Value" or "Result" or similar. So, basically the value (or result) of True or False is True (second table), just like the value (or result) of 3 + 5 is 8. It's like arithmetic, nothing more.




ANSWER 4

Score 2


Truth tables are just like times tables. They empower you to find the result of a logical operation based on the inputs. In the analogy, multiplication gets replaced with conjunction (and), disjunction (or), etc.

It may help you to think about some underlying attitudes of the truth-table approach:

  1. The meaning of "and" and "or" does not change based on context. This is radically different from natural English, where such statements as "say that again and I'll break your legs," and "Are you gonna eat that or throw it in the trash?", the meaning of "and" and "or" is influenced by their surroundings.

  2. A logical statement involving and, or, not, etc., is true or false, depending only on the truth or falsehood of the component statements. Any other meaning of the statements, or relationship between meanings, is irrelevant. Thus "1=1 or you're stupid" is simple, true, and unproblematic, whereas in normal conversation it might provoke some objection.

Based on these two presumptions, it's possible to say categorically whether any statement of the form "A or B" is true, using only information about whether the individuals ("A", "B") are true. This is what the truth table does.

Hope this helps.