0

Every precedence table I look at for Python states the equality(==) operator is evaluated before the 'or' keyword. Why then is '1' printed for this code? I thought the expression would evaluate as false, and the if block wouldn't run.

number = 1
if number == 0 or 1:
    print(number)

How about the following code? Why isn't 'False' printed? Instead, the integer 1 is printed. It confuses me how the variable 'a' gets assigned as 1 instead of as a False boolean value.

number = 1
a = (number == 0 or 1)
print(a)
  • 2
    Right, `==` *is* evaluated first. So `1 == 0 or 1` -> `(1 == 0) or 1` -> `False or 1` -> `1`. – jasonharper Oct 06 '22 at 01:26
  • 1
    Why do you expect `if number == 0 or 1:` to not be true? `or` means only one of the conditions needs to be true. – Mark Oct 06 '22 at 01:27

3 Answers3

1

Well, you are correct. == does evaluate before or. In the case of if number == 1 or 1, what happens is that python evaluates numbers that aren't 0 as True. So the first "argument" (number == 1) will evaluate to False, while 1 will evaluate to True. And False or True will always be True.

The other case (a = (number == 0 or 1)) is a little more tricky. In the case of having an assignment of a variable to multiple expressions chained with or, Python will assign the variable to the first expression that evaluates to True and if it doesn't find any, it will assign to the last expression.

In this case, the first expression (number == 0) is False while 1 on the other hand is True. a therefore gets assigned the value of the second expression, which is 1.

chrille0313
  • 111
  • 4
0
number = 1
if number == 0 or 1:
    print(number)

In above if statement, you have two conditions. It always tries to find the boolean expression of the given conditions and block will get executed only when it finds True.

number == 0

First condition evaluates False since the number is 1

boolean(1)

Second condition evaluates True which is why block of code executed and 1 is printed.

Same happened with the other part of your code.

number = 1
a = (number == 0 or 1)
print(a)

Just remember that our code tries to find the True values. Hope, it helps.

Murali S
  • 61
  • 3
0

See python docs Boolean operations

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

Going by your example:

number = 1
if number == 0 or 1:
    print(number)

# disected
print(number == 0) # prints False
print(False or 1)  # prints 1 as it is a truthy value

In your boolean operation the equality operator == is evaluated before the or keyword. As number == 0 evaluates to False the right side of the or operator gets evaluated. 1 is a true statement so 1 gets returned.

But what if both sides evaluate to False or have a falsy value?

number = 1
if number == 0 or 0: 
    print(number)     # won't print

As number == 0 evaluates to False, 0 is evaluated. That is a falsy value, so the or operation returns False, or in this case the falsy value 0 which evaluates to False in the if statement.

Wolric
  • 701
  • 1
  • 2
  • 18