-2

why would it return 77 in case 3? isn't it a bool statement? Note that case 1 and 2 return False. What is the difference between case 2 and 3?

7 >= 77 and 77 
Out[313]: False

7 >= 77 and 7
Out[314]: False

7 >= 7 and 77
Out[315]: 77

7 >= 8 and 77
Out[318]: False

Thanks

regarding @Kasramvd's comment that my question is the exact duplicate of the question from another user (see below)

it is not obvious at all to me, a novice, that my question is the same as the below. An expert can easily see that the same answer apply to both. After reading answers, i do see a link. Before getting the answers, no way I could've known.

i am a novice in programming. And it was my first time joining stack over flow. And this was my first question on stackoverflow. It appears that stackoverflow is not novice friendly. Thanks for blocking me to ask more questions.Just found that out too.


another user's question

I am trying to understand this code from someone else's project. If you want the context it's here: https://github.com/newsapps/beeswithmachineguns/blob/master/beeswithmachineguns/bees.py#L501

IS_PY2 is just a boolean variable, True if the Python major version is 2. I know that a non-empty string is True, but for some reason I don't understand openmode is assigned either 'w' or 'wt' rather than True or False.

openmode = IS_PY2 and 'w' or 'wt'
openkwargs = IS_PY2 and {} or {'encoding': 'utf-8', 'newline': ''}
Huy Nguyen
  • 73
  • 5
  • It's not so simple: http://stackoverflow.com/questions/36550588/assigning-string-with-boolean-expression/36551857#36551857 – Andras Deak -- Слава Україні Sep 28 '16 at 22:21
  • see edit of original question for clarifications @Kasramvd – Huy Nguyen Sep 29 '16 at 08:15
  • @HuyNguyen Nobody blocks you from asking more questions. This is the best way that this community tends to be mange. And every newcomer should read the [documentation first](http://stackoverflow.com/help/how-to-ask), before getting started. – Mazdak Sep 29 '16 at 11:00
  • @Kasramvd, there are different problems that have the same root cause. Like the ones above, my questions were on bool statements with comparison signs (>, <, ==,.....) that normally return true or false (as one would intuitively expect), the other are with normal statements. I am sure there are other better examples. One can say 1 thing can manifest itself in different forms. People see the different forms and ask for help. How is it a duplicate, even if the same answer applies? – Huy Nguyen Sep 29 '16 at 18:17

2 Answers2

0

This has to do with the fact that and short-circuits, and returns the truthy value instead of True or False.

In:

7 >= 77 and 7

7 >= 77 equates to False, so the second operand of and is never evaluated, so it becomes False.

In:

7 >= 7 and 77

7 is equal to 7 (obviously), so the second operand of and is evaluated. Since 77 is truthy, it's what's returned.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
0

I believe the mechanics of and statements in python work like this.

The conditions in the and statements are evaluated sequentially. If a falsy thing is found, then this is returned as it is. Otherwise, the last thing in the and conditions is returned.

In Python things have the notion of being truthy or falsy without necessarily being boolean variables. Thus, the and just returns either the first thing that is falsy, or the last condition. In this case, it is retuning 77 and if you put this in a conditional, it would be treated as a True because 77 is truthy.

For ex, if you do

7>=7 and False and 77 # ===> False
7>=7 and 0 and 77 # ===> 0
7>=7 and [] and 77 # ===> []

So you see it doesn't return True/False necessarily, it just returns the first falsy thing it sees or returns the last thing.

gowrath
  • 3,136
  • 2
  • 17
  • 32