0

What does 'a' and 'b' in Python mean and why does it equal 'b'? Why does it not equal 'a'?

>>> 'a' and 'b'
'b'
user2357112
  • 260,549
  • 28
  • 431
  • 505
David
  • 2,926
  • 1
  • 27
  • 61
  • 1
    Can you provide a code example to explain what you're seeing? – Kyle Willmon May 06 '19 at 19:52
  • 3
    For the same reason `True and False` equals `False`, or `'True1' and 'True2'` equals `'True2'`. – Charles Duffy May 06 '19 at 19:53
  • 1
    @KyleWillmon: That's the code example. `'a' and 'b'`. It evaluates to `'b'`. – user2357112 May 06 '19 at 19:53
  • Please elaborate , your question and title are too vague. **Edit:** Sorry, I get it now. – mbhargav294 May 06 '19 at 19:53
  • @mbhargav294, it's not particularly vague, this is a duplicate asking about short-circuiting boolean evaluation. We have plenty of entries on the subject already in the knowledge base. – Charles Duffy May 06 '19 at 19:53
  • 2
    @CharlesDuffy it's definitely not vague, but `True and False` equals `False` because of boolean evaluation, not because `False` is second. – Swagga Ting May 06 '19 at 19:55
  • 1
    @GalSivan, yes, but the way `and` works, if the first operand is **truthy** (a different thing from being `True`) it evaluates to the second, otherwise it evaluates to the first. The net effect of this is identical to the boolean operation you describe, but it also lets the individual values retain their identities rather than coercing them to the `True` and `False` singletons (were the values not those singletons already). – Charles Duffy May 06 '19 at 19:58
  • @GalSivan, ...you'll note that `1 and 0` evaluates to `0`, not to `False`, just as `False or 'Hello'` evaluates to `'Hello'`. – Charles Duffy May 06 '19 at 19:59

2 Answers2

2

From the Pycharm docs:

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Because 'a' is not False 'b' is evaluated and returned.

Also nice to know. What is evaluated as True and what as False:

the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.

Uli Sotschok
  • 1,206
  • 1
  • 9
  • 19
  • 2
    The second part „Because 'b' is False neither 'b' is returned.“ is wrong and irrelevant. I would remove it and change the rest to 'b' is evaluated and returned. – eckes May 06 '19 at 20:08
1

Either 'a' or 'b' would be an acceptable answer to 'a' and 'b' (since both are truthy), but only False would be an acceptable answer to 'a' and False, just as only 0 would be an acceptable answer to 'a' and 0 (since the result of this evaluation must be false-y to be logically correct).

Having a short-circuiting boolean evaluation follow the right-hand path when the left-hand is true allows there to be a single rule that applies in all cases.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441