Python "and" behavior

G

goldtech

Could you explain or link me to an explanation of this? Been using
Python for a while but not sure I understand what's happening below.
Thanks.

 
M

Mensanator

Could you explain or link me to an explanation of this? Been using
Python for a while but not sure I understand what's happening below.
Thanks.





0


Does this help?
False
 
M

MRAB

Gary said:
Python's Boolean operators don't turn arbitrary values into True and
False values. If you use it in any conditional, you'll get the same
result as if it did, but it is occasionally it's nice to get the actual
values used in the "and" instead of having the value distilled down to a
True/False.



These are the Boolean operations, ordered by ascending priority:

Operation Result Notes
|x or y| if x is false, then y, else x (1)
|x and y| if x is false, then x, else y (1)
|not x| if x is false, then |True|, else |False| (2)
The Pythonic table would be:

Operation Result
|x or y| x if x else y
|x and y| y if x else x
|not x| False if x else False

:)
 
E

Ethan Furman

MRAB said:
The Pythonic table would be:

Operation Result
|x or y| x if x else y
|x and y| y if x else x
|not x| False if x else False

:)

That last should be
|not x| False if x else True

~Ethan~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top