-1

Here the code.

a = False
if a == True or True:
   print "Hell yeah,I'm genius"
else:
   print "shit,I am a fool"

Output is 'Hell yeah,I'm genius'

Pasha
  • 6,298
  • 2
  • 22
  • 34
  • 2
    There is very little to explain here. Anything `or True` will evaluate to `True`, meaning the first line will print. – Karin Aug 30 '16 at 04:41
  • @karin Thank you... I'm a python newbie.So I have assigned "False" to "a" isn't that mean a is 0 ? And True or True==1 ? Then i thought shit,I am a fool is the output.. – Gayantha Akalanka Aug 30 '16 at 04:46
  • The problem is with the "or True". Remove this part and you will get "shit I am a fool". Due to "or True" every time the if condition is met and you are getting "Hell yeah I',m genius" – Dinesh Pundkar Aug 30 '16 at 04:49
  • Please go through this link for more info AND/OR in python - http://stackoverflow.com/questions/10149747/and-or-in-python/10149756#10149756 – Dinesh Pundkar Aug 30 '16 at 04:50
  • @DineshPundkar Is there a big difference between "True" and "True or True"..Aren't they both equal to boolean "1" ? – Gayantha Akalanka Aug 30 '16 at 04:52
  • 2
    `False or True` will evaluate to `True`. So, even though `a == True` is False, the `or True` will make the entire condition `True` regardless. – Karin Aug 30 '16 at 04:52
  • what your `if` condition is saying is "if the value of A is True, or if True is True, print 'hell yeah I'm a genius'". Truth is, unsurprisingly, always True. – n1c9 Aug 30 '16 at 04:53
  • @Karin Thank you very much for your explanation...@n1c9 Thank you...!!! – Gayantha Akalanka Aug 30 '16 at 04:56
  • Addint to @Karin above comment . In case of "a==True or True", first "a==True" is evaluated which will return False. Then, remaining will be "False or True". Therefore, False or True will evaluate to True. – Dinesh Pundkar Aug 30 '16 at 04:56
  • @DineshPundkar Thank you very much..!!! – Gayantha Akalanka Aug 30 '16 at 04:58

2 Answers2

2
a ==True or True 

Consider True is 1 and 0 is False.

Since a is set to False (a=False in first statement of code), the first part 'a==True' i.e. 0 ==1 will return 0 (False).

Then remaining will be False or True since 'a==True' is False. So it will be like 0 or 1 (False or True).

We know that

  • 0 AND 0 = 0
  • 1 AND 0 = 0
  • 1 AND 1 = 1
  • 0 OR 0 = 0
  • 0 OR 1 = 1
  • 1 OR 1 = 1

So in your case, 0 OR 1 will result to 1 i.e. True.

Summary :

a == True or True => False or True => True

That's why "Hell yeah,I'm genius" will be printed.

Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
-1

Anything True , it will run that section ...

if True:

   print "Hell yeah,I'm genius"

else:

   print "shit,I am a fool"

This one also returns "Hell yeah,I'm genius"

Julien
  • 13,986
  • 5
  • 29
  • 53
Pranjay Kaparuwan
  • 881
  • 1
  • 11
  • 17
  • @DineshPundkar If you're in Python2, then you can say `True = False` before running this example, and it'll give you the desired output ;) – Pasha Aug 30 '16 at 05:32