0

I came across this expression this expression in the Kaggle introduction to Data Science with Python. I'm having a hard even understanding what this means. How can you determine that this expression is true?

J.DF
  • 23
  • 7
  • Python documentation gives you the operator precedence, including that "and" is higher than "or". Where are you confused? – Prune May 13 '21 at 16:05
  • Does this answer your question? [Does Python support short-circuiting?](https://stackoverflow.com/questions/2580136/does-python-support-short-circuiting) – Lucas May 13 '21 at 17:32

3 Answers3

6

Since and has a higher precedence than or the expression

True or True and False

is evaluated as

True or (True and False)

which in turn evaluates to

True or False

which is straightforwardly True.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • 1
    @J.DF If you are not sure why `True or False` is `True`, please do own research about [boolean logic](https://en.wikipedia.org/wiki/Boolean_algebra). This is not Python (or even programming, really) specific. – DeepSpace May 13 '21 at 16:11
1

the notion true or true and false can be re-written as (true) or (true and false). and means both arguments should be true in order for the answer to be true, therefore, the (true and false) yields FALSE. or on the other hand means at least one of the arguments must be true. so true or false yields TRUE.

Watch more tutorial about this on youtube, there's a lot of helpful explanation there :)

cheese
  • 73
  • 6
0

In Python when the condition is evaluated:
if True or True and False:
It sees that the first expression is true, and since it is combined with an 'or' it doesn't matter what the rest of the condition is.

It doesn't look good to write a conditional like that, don't combine or and and operators without parenthesis so it is clear what you are doing if True or (True and False):
This is essentially how it will be interpreted, and is more clear with parenthesis.

This is short circuit evaluation, it's done in Python/C/C++/Java.. though Java doesn't short circuit the single & or | operators. Details regarding precedence, order of evaluation, and whether conditions short circuit are in the language specification.

With short circuit evaluation:
If a series of statements are connected with ORs then it knows the condition is satisfied when it finds the first valid statement.
If a series of statements are connected with ANDs then it knows the condition cannot be satisfied when it finds the first invalid statement.

Using this you can do things like gaurd bound checking while checking values at the same time:
if (i<arraysize && array[i]==1)
The second part will only be evaluated if i is within the bounds.

You can reverse this too to provide a similar protection:
if (i==0 || array[i-1]==1)
if the index is zero then the condition is satisfied, otherwise there is an element behind it and you can check the element behind it.

Chase LP
  • 229
  • 2
  • 7
  • This has nothing to do with short-circuiting. `True or False` is always `True`, even if short-circuiting is not implemented – DeepSpace May 13 '21 at 16:14
  • It does short circuit, so the second condition is never evaluated. If it were then it would be True or False. – Chase LP May 13 '21 at 16:17
  • Yes, it is evaluated. `True or False` is already `True or False` so I don't really understand your comment – DeepSpace May 13 '21 at 16:19
  • Try this to see for yourself: array=[] if True or True and array[20]==3: print('hello') – Chase LP May 13 '21 at 16:25
  • What does this have to do with you claiming that `True or False` is `True` due to short-circuiting? – DeepSpace May 13 '21 at 16:27
  • The condition is "if True or (.....)", short circuiting is what is applied. It doesn't matter what is after the first valid condition in this case, and nothing else will even be evaluated. – Chase LP May 13 '21 at 16:32
  • @ChaseLP And it doesn't matter that the (...) part won't be evaluated; if it were evaluated it wouldn't change the result. – Alexey Romanov May 14 '21 at 07:01