1

I am confused with Logical 'And' and 'or' combination in python

I got some idea from Python's Logical Operator And but I couldn't understand combination of 'AND' and 'OR'

   a=3
   print(a%2 and 'odd' or 'even')

I understood how even was printed if a is 2 ie.,
a= 2, 2%2 = 0 => False.
Then Note String object is considered as True so 'odd' and 'even' are True.
So False and True(odd) or True(even) will give be False(even) object.

But when I didn't understand how even works. If 1st object is True then the output should be immediate True without checking other condition(or operations), how is it going further and printing 'even'

narengs7
  • 67
  • 1
  • 9
  • The result of `and` is not (necessarily) a boolean. – Norrius Jul 26 '19 at 17:54
  • The `exp and p or q` construct was a very common pratice back at times where the trinary operator did not ut is roughly equivalent to `p if exp else q`. – Klaus D. Jul 26 '19 at 17:56
  • I think this is a better duplicate target. https://stackoverflow.com/questions/47007680/strange-use-of-and-or-operator/47007761#47007761 – Håken Lid Jul 26 '19 at 18:00
  • Thanks that doesn't completely explains me, but I am able to write the complex once. But I did not get the whole concept why its made like that a simple incline like in c ( `a%2==0 ? 'even':'odd'; `) where `and` seems to be like `?` and `or` seems to be like `:`. – narengs7 Jul 26 '19 at 18:03
  • @Narengs its mainly due to the numbers 1 and 0 being Boolean values on there own. 1 is true and 0 is false. – Ryan Schaefer Jul 26 '19 at 18:04
  • @RyanSchaefer I get the concept of 1 is True, 0 is False and all other objects are considered to be True. But I didn't get the whole concept of 'and' and 'or' thing doing and giving odd, even part – narengs7 Jul 26 '19 at 18:08
  • Well no, not even that Ryan. Naren, it's because `and` does not imply that the output will be a boolean. I actually encourage you to only run this portion: `a%2 and 'odd'` And see what outputs you get. you'd be surprised. Also, read the duped link explaining `and` and `or` – Paritosh Singh Jul 26 '19 at 18:08
  • @HåkenLid I did mention that in the link above but it only explains only about AND not who combinations – narengs7 Jul 26 '19 at 18:09
  • @ParitoshSingh for a= 1, I get 'odd' . for a=2, I get 0 – narengs7 Jul 26 '19 at 18:11
  • @Narengs now add the last bit in. pair up `'odd' or 'even'` and `0 or 'even'`. Do you see what's happening? Run and see the outputs * (oops, made a typo with 0) – Paritosh Singh Jul 26 '19 at 18:13
  • @ParitoshSingh this isn't really surprising for functional languages that use and and or operators more of as return controllers. When doing 0 and "something" the and statement is short circuited and therefore the first thing is returned. However, if the first part is evaluated to true the program has to evaluate the second part and return that since it is the last thing evaluated. This is why it works for even a second value that is false. Meanwhile on the or side of things, or is not short circuted by a false value and thus it has to keep evaluating until a true and thus "even". – Ryan Schaefer Jul 26 '19 at 18:13
  • @RyanSchaefer I suppose all i can say is that it was surprising to me. This was not something that came obvious when i was picking up python. And i suspect that's what the OP is dealing with too, especially since they mentioned they understand the concept of truthiness, in the question itself. Which only really leaves out the fact that one isn't getting booleans out of it. – Paritosh Singh Jul 26 '19 at 18:18
  • @ParitoshSingh if you really want to get wild try this: `print((True and [1] and "thing" and False) or [] or 0 or None or "final")` – Ryan Schaefer Jul 26 '19 at 18:20

1 Answers1

2

If you are thinking of trying to emulate JavaScript or other languages ternary operators, you have to do it something like this:

print("even" if a%2 == 0 else "odd")

edit: even though this question has been closed (and I don't believe it should be) I will edit it with the understanding about what your question is actually asking.

In python bool(1) == True and bool(0) == False notice how when you modulo 2 these are the only two possible values you can get.

Now go back to your origional print statement:

print(a%2 and 'odd' or 'even')

Combine the fact that 1 is true and 0 is false with the fact that the first condition of a statement ... and [...] or ... returns if the statement is true and the second returns if it is false.

It is then clear to see that when the number is odd causing the number modulo 2 to be 1 it will return the first condition "odd" and when it is 0 it will cause the second condition to be returned "even".

I hope this explains everything.

Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46
  • I am not sure it is answering the question. For someone who is not very familiar with Python, it is not even clear, why "odd" or "even" should be printed and not "true" or "false", as one would expect from logical operation result (yet it is explained in the answer to the question the OP have linked). – Eugene Sh. Jul 26 '19 at 17:54
  • @EugeneSh. I intitially misread the question. I edited this into my answer – Ryan Schaefer Jul 26 '19 at 18:02