0

I am learning python and have problem with logical operator 'or'. Can't find information and understand why code in various writing interpreted differently (what the difference in #First and #Second examples). So, #First example doesn't equal #Second, when operator 'and' equal in all variants of code

# First
a=35
b=35
if a or b>35:
    print('First')

'''with code like above python have printed 'First' even if statement false, like i understand it. 
But in other examples, statement are false too, and 'print' wasn't done, like it must be.'''

# Second
c=35
d=35
if c>35 or d>35:
    print('Second')

#Third    
e=35
f=35
if e>35 and f>35:
    print('Third')

#Fourth    
g=35
h=35
if g and h>35:
    print('Fourth')
2
  • Please take look at python's operator precedence Commented May 1, 2020 at 13:34
  • You need to specify the condition for both of the conditions so instead of if a or b>35: it would be if a >35or b>35: Commented May 1, 2020 at 15:43

4 Answers 4

5
a or b > 35

is equivalent to

a or (b > 35)

and a is truthy because all integers except 0 are truthy. You can verify:

>>> a or b > 35
35
>>> (a or b) > 35  
False

See truth testing and operator precedence.

Also note that (a or b) > 35 will not become True if any of a or b is greater than 35.

(a or b or c or ... or n) > 35

is true only if the first non-zero number in the disjunction is greater than 35

Sign up to request clarification or add additional context in comments.

Comments

2

"if a" will always evaluate to true for an int if a != 0.

Comments

1

When you use a number X directly in a condition, Python implicitely converts it to a boolean value by applying the comparison X != 0

So, for your examples:

First

a=35
b=35
if a or b>35:        # same as if a!=0 or b>35:   True or False --> True
    print('First')

Second

c=35
d=35
if c>35 or d>35:     # False or False --> False
    print('Second')

Third

e=35
f=35
if e>35 and f>35:    # False and False --> False
    print('Third')

Fourth

g=35
h=35
if g and h>35:       # same as if g!=0 and h>35:  True and False --> False
    print('Fourth')

If you're looking to express a condition to test that either a or b are greater than 35, you cannot write it as a or b > 35.
While it may be clear to you in English, it is not how Python reads it.

Python requires that you be more explicit, so you will have to repeat the > 35 :

a > 35 or b > 35.

If you want to avoid repeating the 35, here's a trick you could use:

if max(a,b) > 35:  
   # either a or b is > 35  (i.e. the highest of the two is > 35)

if min(a,b) > 35:
   # both a and b are > 35  (i.e. the lowest of the two is > 35)

Comments

0

Actually you have to split it to better understand it. Let's take the first case:

a=35
b=35
if a or b>35:
  print('First')

In this case, your if-condition is composed of two parts a and b>25, and the or operator means you need at least one of them to be True to print First.

So what does if a means? actually that means if a is equal to a value different than None, 0, "" and False. So in your case, a is equal 35 thus the evaluation of the first part is True. The second part is evaluated to False since b is not greater than 35. And this is why it prints First.

For the fourth example, you need both of them to be evaluated True to have the print statement executed.

I hope this helps you to better understand it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.