+ 2
Guys what about this section of python 3 tutorial, Is it correct?
I am learning python 3 from solo learn found this... tutorial say == have more precedence than 'or' But... Details:- first print(False == printme() or True) when this get evaluated output is Output: printed True printme function called to evaluate the first expression which is False == printme() but second print(True or False or printme()) now when this get evaluated... Output: True printme() was not called to evaluate the second expression which should be evaluated first because tutorial said == have greater precedence than or. Now run code, shared in this post. https://code.sololearn.com/cjKNgtn6XDco/?ref=app
12 Respuestas
+ 6
Yeah its cool👍👍
+ 3
ifl
Both statements don't print true...
first print
printed
True
Note:- 'printed' word
that output is totally different than the second one...
which only prints the
True
isn't?
+ 3
Oh okay, sorry for misunderstanding your question. Good question by the way!
Why this happens is because of how the 'or' operator works, and not because of operator precedence.
-----------------------
You see, the 'or' operator returns True if either one of its inputs is True. So:
True or True --> True
True or False --> True
False or True --> True
False or False --> False
-----------------------
If you look closely, you can notice that If the first operand (left to the 'or') is True, then you don't really have to look at the second operand, because the answer is True either way.
That is exactly what Python does. If the first operand is True, it returns True without evaluating the second operand (right to the 'or').
-----------------------
In your case,
(True) or (False == printme())
As soon as Python sees True before the 'or', it knows the second expression need not be evaluated, therefore printme() is not run.
-----------------------
Hope this made sense :)
+ 2
ifl tutorial say that == have greater precedence than or...
so in the code
second print statement
which is
print(True or False == printme())
should first print the
printed
True
But it don't
+ 2
Just A Rather Ridiculously Long Username
both print statement should give the same result.
But they don't
+ 2
ifl
oh thanks,
now it's making sense.
+ 1
Vikas Gola It seems to me that the code does the right thing.... what result do you expect and how does it differ to the results you see?
+ 1
ifl
first print statement is printing...
printed
True
and the other one...
True
check again!
+ 1
Vikas Gola Ah yes. This is because the evaluation of the Boolean logic is left to right.
Because of operator precedence, the statement is equivalent to
true or (false==printme())
but since the left part of the 'or' is true, the right part is not evaluated.
(operator precedence and order of evaluation are different things)
+ 1
(Just A Rather Ridiculously Long Username just beat me to it ;-) )
0
Just A Rather Ridiculously Long Username ,Both statements give the same result (true) when I execute on my phone.
As expected.
0
Vikas Gola yes, that's correct. Both statements print true.
And this is the correct answer.