1

In answer to a question about Python and/or logic, Spacetoast wrote:

x and y returns true if both x and y are true.

x or y returns if either one is true.

I manage to confuse myself by using "or" when it should have been "and". Without having to drawing up truth tables, is there an mnemonic to help remember the correct usage of and/or?

Community
  • 1
  • 1
Mr Mystery Guest
  • 1,464
  • 1
  • 18
  • 47
  • 1
    I would have thought that the best mnemonic for remembering how "and" and "or" work (for English speakers at least) is remembering what the words "and" and "or" mean in English. The words were not chosen to be difficult to understand. – khelwood May 03 '16 at 10:08

3 Answers3

3

The confusion from the pointed answer to that question come from the fact that he says the OR contains AND which is true in your case. There's is nothing to remember here, if you use or operator and at least one of the two expressions is True then it returns True if none of the two expressions are True it returns False

This means that if both x and y are True the or will be True too because as soon as the first expression x is True there's no need for evaluating the second one.

On the other hand, if you're using and and the first expression doesn't evaluate to True, there's no need to evaluate the second one, since it's impossible for both of them to be True anymore.

enter image description here

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
  • 1
    Regarding your first paragraph: this is different from `xor`, which *does* literally mean "either one *or* the other – but not both". A bit like asking, "what do you want for dessert, ice cream or pudding?" "I want both!" – mum should have asked "ice cream `xor` pudding". – Jongware May 03 '16 at 10:38
3

Not a true Mnemonic, but NOAH has helped my students in the past.

How many inputs must be true?

None => Nor

One => Or

All => And

Honestly => Heed the truth tables

To be honest the skill of being able to use a truth table is worth persevering with. (Note there is not a NOR operator in python, its a Not-or).

Grimley
  • 107
  • 10
1

there is no need of mnemonic for this remember this by a simple concept:

or -> any one true
and -> all true

true or false = True

true and false = False

joginder singh
  • 41
  • 1
  • 1
  • 7