4

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??

6
  • 13
    Am I the only one wondering why someone would memorize these? Commented Dec 15, 2010 at 1:34
  • 2
    The book is a joke. At least, I think it is; it's come up here before. I can't quite believe people actually try to learn Python from it... do they? Commented Dec 15, 2010 at 1:37
  • 1
    True/False are boolean values. These tables describe how to combine boolean values. A condition is never both true and false (even in real life, unless you're into mysticism), and is either true or false (this one isn't really true in real life, due to oversimplifications). Commented Dec 15, 2010 at 1:40
  • 4
    When they say it's "the hard way", they aren't kidding. Commented Dec 15, 2010 at 1:49
  • I guess if you don't like these then you'll really hate XOR's... Commented Dec 15, 2010 at 2:34

9 Answers 9

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?

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. This answer has been the most helpful one so far, however I don't see why if a is true and b is true, then a AND b is true. Same thing for both false. See my original post, which I have updated, for an image showing which ones I don't understand (unchanged from when I posted this question).
@tobe: a and b is defined to be true exactly when a and b both are.
Okay, and I could just memorize that, but it would be easier if i could understand why.
@tobeannounced: that's the definition of AND; that's what it does. a AND b results in True exactly and only when a is True and b is also True.
Fantastic! The edit is a great way of thinking about it! Before I saw that, I had learned them with 5 minutes of memorization, but still extremely useful. Thanks again
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

10 Comments

I don't understand why that works with: False and True = False (is it because it can't be both?), True and True = True (what if it is false?). I get the bottom two tables fine
and is an operator, just like all the others: it takes two values and returns one. In this case it returns True if both inputs are True and False otherwise.
Personally I'd use is instead of =; it reads better and is valid in Python: True or False is True. @tobeannounced: "is it raining and is it not raining?" Obviously not; True and False is False (or False and True is True if it's not raining where you are.)
Putting statements like True or True = True inside code formatting may confuse people. You can't use that in Python code since True or True is not an L-value
Thanks again. I have updated the image to show the ones that I don't get - all of which are based on the same theory I believe.
|
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.

Comments

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.

Comments

2

this isn't that hard to understand. its a preposition.

lets say someone says about me: "He has an apple in one hand AND an apple in the other hand" but I only have an apple in one hand then the statement that person just made is false.

Therefore the preposition: a AND b is saying that a and b are both true, so if a is false for example then that preposition is false.

the trickier one is OR. The OR preposition says that at least one of the 2 must be true but if they are both true then that statement is still true because "at least 1 of the 2 are true"

so that would be like someone saying: "I know that guy has an apple in at least 1 of his hands" they wouldn't be lieing even if i had an apple in each hand right? therefore their statement is true.

Comments

1

Left side question, right side truth of answer.

  • true == true | true
  • true == false | false

Comments

1

Why are you trying to memorize these?

Doesn't it make logical sense to you that (for example) 1 == 0 is not true?

2 Comments

LOL - Thats why its the hard way
The truth value of a comparison is probably obvious. The truth value of boolean operations is hardly. Some comply with intuition, some don't (just think of implication, False => False is True). I remember memorizing the outcome of boolean operations, and I wish more programmers did, looking at their branch conditions.
1

In all of the heading above, apply whiteout to the "True?" heading, and write in "Result". Then it's much clearer.

Comments

0

There once was a guy named Boole. He was interested to "calculate" with truth values, like others calculated with natural number. As he was a mathematician, he knew what he had to do to define a new "calculus" or "algebra": Find a set of values, and find a set of operations upon these values, such that applying an operation to some value(s) yields again a value of the base set.

The set of values for his new algebra just consisted of two, True and False. Then he invented the operations, "and", "or", and "not". To be consistent with mathematical rules of algebra, the operations had to work on any input from the value set (whether they have two inputs or one), and produce a result from the value set. And as there are no better means to achieve this in this case, for each operation he listed all the possible argument values and assigned them a result. That's what you see in the truth tables.

Now, as you have found out, the assignment of the results is a bit arbitrary. Some can be motivated by common sense, some can't. (Other posters have tried to motivate the results of some of your "red dots"). Don't struggle too hard with it. If you can find a motivation in common sense logic, then fine, if not, don't worry. Boole had to patch up his tables somehow, to make his operations "total". There is also no real logic behind giving the outcome of "3+4" the name "7". That's why we memorize these basic things :).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.