Conditionals And Control Flows
Jussi Piitulainen
jussi.piitulainen at helsinki.fi
Wed May 4 11:10:14 EDT 2016
Cai Gengyang writes:
> I am trying to understand the boolean operator "and" in Python. It is
> supposed to return "True" when the expression on both sides of "and"
> are true
>
> For instance,
>
> 1 < 3 and 10 < 20 is True --- (because both statements are true)
Yes.
> 1 < 5 and 5 > 12 is False --- (because both statements are false)
No :)
> bool_one = False and False --- This should give False because none of the statements are False
> bool_two = True and False --- This should give False because only 1 statement is True
> bool_three = False and True --- This should give False because only 1 statement is True
Yes.
> bool_five = True and True --- This should give True because only 1 statement is True
No :)
> Am I correct ?
Somewhat.
In a technical programming-language sense, these are "expressions", not
"statements". Technically, if the first expression evaluates to a value
that counts as true in Python, the compound expression "E and F"
evaluates to the value of the second expression.
Apart from False, "empty" values like 0, "", [] count as false in
Python, and all the others count as true.
But it's true that "E and F" only evaluates to a true value when both E
and F evaluate to a true value.
Your subject line is good: Python's "and" is indeed a conditional,
control-flow operator.
More information about the Python-list
mailing list