2

I am currently in the course of learning Python 2.7 and have come across the Equality and Boolean operators

My question is:

Why False and 1 is False but True and 1 is 1

Likewise, False or 1 is 1 but True or 1 is True

Can someone kindly explain why this is happening

Many thanks

1 Answer 1

3

and returns the first 'falsy' (False, zero, empty string or list, etc.) value it sees, or the final value if none were falsy. Further values are not even evaluated, since they can't change the result.

or likewise returns the first 'truthy' (True, non-zero, non-empty string or list, etc.) value it sees (or the final one if there were none), and doesn't evaluate the rest.

This behavior is sometimes more convenient than strictly returning only True or False.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Jason. I was missing out on the fact that any number other than 0 can also be considered as True.
Btw, going by the above logic, why do you think False or 0 returns 0. In this case both of them are actually Falsies, isn't it?
@pb_ng Note that the answer does state for the case of or: "or likewise returns the first 'truthy' [...] value it sees (or the final one if there were none)". There are no true values in False or 0, so the last one, 0, is the result.
Thanks a lot for your explanation. It helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.