2
first = int(input('first int: '))
second = int (input('second int: '))
result =0
if first and second:
result =1
elif not first:
    result =2
elif first or second:
    result=3
else:
    result=4
print(result)

when I enter 1 and 0, the result is 3. I would appreciate if anyone could add some explanation.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356

2 Answers2

2

You are using or- it means the statement will return True when it first finds True. When you say 5 or 9, both 5 and 9 represent truism (as does any non-zero value). So it returns the first - 5 in this case. When you say 9 or 5, it returns 9.

EDIT: k = 1 or 0 would evaluate to True since 1 represents truism. Thus, as per your code, result is 3

gravetii
  • 9,273
  • 9
  • 56
  • 75
0

In many program languages, the boolean operations only evaluate their second argument if needed for their outcome. These called short-circuit operator. And in python, according the docs, it returns:

x or y  :   if x is false, then y, else x
x and y :   if x is false, then x, else y
atupal
  • 16,404
  • 5
  • 31
  • 42