Please explain the output of this python code

时间:2016-08-30 04:38:37

标签: python

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'

2 个答案:

答案 0 :(得分:2)

a ==True or True 

考虑True为1且0为假。

由于a设置为False(在第一个代码语句中a = False),因此第一部分' a == True'即0 == 1将返回0(假)。

然后剩下的将是False或True,因为' a == True'是假的。 所以它将像0或1(假或真)。

我们知道

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

因此,在您的情况下,0 OR 1将导致1,即True。

摘要:

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

这就是为什么"地狱耶,我是天才"将被打印。

答案 1 :(得分:-1)

一切都是真的,它将运行该部分......

if True:

   print "Hell yeah,I'm genius"

else:

   print "shit,I am a fool"

这个也会返回"Hell yeah,I'm genius"