pb_ng
pb_ng

Reputation: 361

Python 2.7 Boolean Operators Logic

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

Upvotes: 2

Views: 755

Answers (1)

jasonharper
jasonharper

Reputation: 9587

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.

Upvotes: 3

Related Questions