0

Can someone please explain why the output is A for following code.

a = True
print (('A','B')[a == False])
  • 3
    What steps have you taken to analyse this yourself? – quamrana Oct 06 '20 at 08:02
  • 2
    is that `a = True` or `==`? – Tibebes. M Oct 06 '20 at 08:04
  • @Tibebes.M, Sorry it was a = True. Corrected that now. – kartik sharma Oct 06 '20 at 08:07
  • @Tomerikoo, This does answer my question, Thanks. But one more doubt, If we are using a True/False condition for indexing, like what is used here. So, it will never return an element with Index 2 or above, right? For ex. print (('A','B','C','D')[a == False]) - I can never get 'C' or 'D' with this True/False Condition ? – kartik sharma Oct 06 '20 at 08:12
  • No. If you read the linked answer, `False` has the value of 0 and `True` the value 1. So obviously no, you can't index 2 or 3, unless you do `1 + a == False` – Tomerikoo Oct 06 '20 at 08:14

1 Answers1

2

Assuming first line is a = True, a == False evaluates to false, therefore boolean value of 0. Therefore your expression is same as print(('A', 'B')[0]) which prints 'A'

Ach113
  • 1,775
  • 3
  • 18
  • 40