+ 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

2nd Jun 2018, 7:54 PM
Vikas Gola
Vikas Gola - avatar
12 Réponses
+ 6
Yeah its cool👍👍
2nd Jun 2018, 8:05 PM
Scorpia Rising🎩
Scorpia Rising🎩 - avatar
+ 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?
2nd Jun 2018, 8:23 PM
Vikas Gola
Vikas Gola - avatar
+ 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 :)
2nd Jun 2018, 8:29 PM
Just A Rather Ridiculously Long Username
+ 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
2nd Jun 2018, 8:10 PM
Vikas Gola
Vikas Gola - avatar
+ 2
Just A Rather Ridiculously Long Username both print statement should give the same result. But they don't
2nd Jun 2018, 8:13 PM
Vikas Gola
Vikas Gola - avatar
+ 2
ifl oh thanks, now it's making sense.
2nd Jun 2018, 8:31 PM
Vikas Gola
Vikas Gola - avatar
+ 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?
2nd Jun 2018, 8:06 PM
ifl
ifl - avatar
+ 1
ifl first print statement is printing... printed True and the other one... True check again!
2nd Jun 2018, 8:18 PM
Vikas Gola
Vikas Gola - avatar
+ 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)
2nd Jun 2018, 8:30 PM
ifl
ifl - avatar
+ 1
2nd Jun 2018, 8:31 PM
ifl
ifl - avatar
0
Just A Rather Ridiculously Long Username ,Both statements give the same result (true) when I execute on my phone. As expected.
2nd Jun 2018, 8:16 PM
ifl
ifl - avatar
0
Vikas Gola yes, that's correct. Both statements print true. And this is the correct answer.
2nd Jun 2018, 8:19 PM
ifl
ifl - avatar