I can’t understand why the code below works like this. (in Python3x)
>>>f = lambda: print('Hello') or print('Hello again')
>>>f()
Hello
Hello again
and also can’t understand like this.
>>>f = lambda: print('Hello') and print('Hello again')
>>>f()
Hello
For me, the first lambda function seems to print the word ‘Hello’ or the word ‘Hello again’ but it print both ‘Hello’ and ‘Hello again’.
The second function seems to print the word ‘Hello’ and ‘Hello again’ but it print only ‘Hello’.
Can anybody explain what is going on in this lambda function?
Thank you for your kind help!
printreturns None (which is Falseish), so python has to evaluate the other operand ofor, but notand, to get the answer.return print('Hello') or print('Hello again')Hellois printed, print returnsNonereturn None or print('Hello again')orreturns True if any of the operands is True. If the first one is True, there’s no need to evaluate the second one. This isn’t the caseHello againis printedreturn None or Nonereturn print('Hello') and print('Hello again')Hellois printed, print returnsNonereturn None and print('Hello again')andreturns True if both operands are True. If the first one is False, there’s no need to evaluate the second one.return False