Problem with or operator

bit of a nooby question but why does this work:

num = 5
if num == 4 or num == 2:
print(“num equals 4 or 2”)

but this does not this always return true:

num = 5
if num == 4 or 2:
print(“num equals 4 or 2”)

im assuming its just a case of thats not how python works?

Oh @Python_Horror, this is what we called “short-circuit” in programming.

The second condition num == 4 or 2 because it evaluates to 2 if num != 4.

A better way is:

if num in {2, 4}:
  print('num equals 2 or 4')

Reference:

1 Like

Thanks for the help!