Question from Sololearn's Python Challenge | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 4

Question from Sololearn's Python Challenge

#1 print("abcded".find("bc") == "cd" in "abcdef") #2 print(1 == True) Output of first print statement is False whereas output of second statement is True. My doubt: When Left side & right side of first statement will evaluate to 1 & True respectively, then why its output is not True. What is the mistake I am doing here?

26th Dec 2020, 4:34 PM
Peter Parker
Peter Parker - avatar
4 Respuestas
+ 5
It is strange, I also doubled checked it in Termux and I am getting the same result you got, but it works if you put parentheses around each statement, I guess explicit is better than implicit? print(("abcdef".find("bc")) == ("cd" in "abcdef"))
26th Dec 2020, 4:51 PM
Steven M
Steven M - avatar
+ 2
Steven M Does the addition of the brackets change the order in which the statements are executed? I never add brackets for comparison. Thanks for illustrating the possibility for an unnoticed bug to mess with the code here!
26th Dec 2020, 5:04 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Calvin Sheen Thomas I assume yes, I took a mathematical approach to it, while also keeping in mind Tim Peters' writing suggestions. Similar coding behavior/style is used in Numpy and Pandas too
26th Dec 2020, 5:42 PM
Steven M
Steven M - avatar
0
Steven M That's cool
27th Dec 2020, 10:59 AM
Calvin Thomas
Calvin Thomas - avatar