-6

I cant figure this out in my head.

>>>print("red" == "blue")
False

>>>print("red" == 3 >= 3)
False 

so why is the following True?

>>> print("red" == "blue" or 3 >= 3)
True

someone put my brain out of its misery

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 3
    When you use the Boolean `or`, only one of the conditions has to be true for it to return true. `3>=3` is true, so the whole expression is true. Check out [Boolean algebra](https://en.wikipedia.org/wiki/Boolean_algebra) for a (lot) more. – MattDMo Oct 16 '20 at 15:24
  • It looks like a dupe when you compare the first snippet to the second. They don't quite match in the same way that the dupe catches people out. – quamrana Oct 16 '20 at 15:30
  • 1
    Never mind, I read through the accepted answer, and if you look hard enough you can see the explanation of logial `or`. – MattDMo Oct 16 '20 at 15:32
  • `print("red" == 3 >= 3)` but that's not any part of your full expression. It's `"red" == "blue" or 3 >= 3` which is evaluated as `("red" == "blue") or (3 >= 3)` which is then `False or True`. – VLAZ Oct 16 '20 at 15:36
  • `"red" == 3 >= 3` is equivalent to `"red" == 3 and 3 >= 3`, which is why you get `False`. – Pranav Hosangadi Oct 16 '20 at 15:42

4 Answers4

1

When you use the Boolean Operator or, only one of the conditions declared has to be true in order for it to return true. 3>=3 is true, so the whole expression is true.

Bialomazur
  • 1,122
  • 2
  • 7
  • 18
0
print("red" == "blue" or 3 >= 3)

Analysis:

"red" == "blue" #--> False

3 >= 3 #-->True

Given that there is an OR operator:

"red" == "blue" or 3 >= 3 #--> False OR True --> True
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
  • Does Python let you use those quotes? Mine (3.7.7 on Win10) complains saying there's an "invalid character in identifier" – Pranav Hosangadi Oct 16 '20 at 15:27
  • @PranavHosangadi no, it doesn't. The reason there are "smart quotes" there is probably because the author is using IE or Edge. – MattDMo Oct 16 '20 at 15:28
0

That's because you used or.

In that specific case, 3 >= 3 returns True, and since it's needs only one True parameter, it returns True, ignoring the other condition.

Gent Bajko
  • 78
  • 1
  • 5
0

print("red" == "blue") #False for obvious reasons as string red is not equal to string blue.

print("red" == 3 >= 3) this is false as red is not equal to 3

print("red" == "blue" or 3 >= 3) this is true because 3 == 3

Divyessh
  • 2,540
  • 1
  • 7
  • 24