Boolean Expressions
Steve D'Aprano
steve+python at pearwood.info
Tue Sep 26 22:20:09 EDT 2017
On Wed, 27 Sep 2017 08:23 am, Cai Gengyang wrote:
>
> I'm trying to understand the logic behind AND. I looked up Python logic tables
>
> False and False gives False
> False and True gives False
> True and False gives False
> True and True gives True.
>
> So does that mean that the way 'and' works in Python is that both terms must
> be True (1) for the entire expression to be True ? Why is it defined that way,
> weird ? I was always under the impression that 'and' means that when you have
> both terms the same, ie either True and True or False and False , then it
> gives True
No, your impression is wrong. Python's AND is the same as boolean AND
everywhere: every programming language that supports boolean AND, in Boolean
Algebra and in logic.
In C++ https://msdn.microsoft.com/en-us/library/c6s3h5a7.aspx
In Javascript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
Boolean algebra
https://en.wikipedia.org/wiki/Boolean_algebra#Basic_operations
Consider that today I spent the day sitting at home watching movies. If I said:
"Today, I climbed to the top of Mount Everest."
That would be False.
If I said:
"Today, I swam across the English Channel."
That would be False.
If I said:
"Today, I climbed to the top of Mount Everest, AND I swam across the English
Channel."
that is still False.
What you are thinking of is best describes as "equals":
False equals False gives True
False equals True gives False
True equals False gives False
True equals True gives True
--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list
mailing list