Conditionals And Control Flows

Chris Angelico rosuav at gmail.com
Wed May 4 10:58:25 EDT 2016


On Thu, May 5, 2016 at 12:41 AM, Cai Gengyang <gengyangcai at gmail.com> wrote:
> 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)
> 1 < 5 and 5 > 12 is False --- (because both statements are false)
>
> 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
> bool_five = True and True --- This should give True because only 1 statement is True
>
> Am I correct ?

Not entirely so, but very close. So long as you stick to the exact
values True and False (or simple conditional expressions, like your
examples), yes, that's what you'd get.

The best way to try these out is the interactive interpreter. On
Windows, look in your Start menu for "IDLE"; on other platforms, open
up a terminal and type "python3". Then just start messing around:

>>> 1 < 3 and 10 < 20
True
>>> 1 < 5 and 5 > 12
False

This is far and away the easiest way to learn how Python works. You
can even play with some other things, and learn how Python's 'and'
operator handles other types of data:

>>> 1 and 4
4
>>> 0 and 3
0

Confused? Keep messing around. Build up a theory as to what's going
on, test your theory, then go check your theory against the
documentation. (Or come and ask here, if you can't find it in the
docs.) Python doesn't mind how much you poke around with it, and you
will learn ever so much more than we can explain!

ChrisA



More information about the Python-list mailing list