Please help me know why this code prints 50?? Shouldn't the answer be a boolean value? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Please help me know why this code prints 50?? Shouldn't the answer be a boolean value?

a=50 b=100 print (a and b)

26th May 2020, 5:46 AM
Shivangi Agarwal
7 Answers
+ 5
Namit Jain print("" and "wow" and "amazing") an empty string evaluates to false. What's the output? Please check the links (😁jk)
26th May 2020, 6:53 AM
Kevin ★
+ 4
It's not printing 50 it's printing 100. See this to know why. https://www.sololearn.com/discuss/2220489/?ref=app Do some research on "short circuiting operands in python" to learn more. Shivangi Agarwal I edited my answer. Take a look at this too (topic 5.1 & 5.2) : https://docs.python.org/2/library/stdtypes.html That's python2 but for py3 it's almost the same thing. Namit Jain Not exactly. Please go through the links given.
26th May 2020, 6:15 AM
Kevin ★
+ 4
Namit Jain print(0 and 2 and 3) 0 evaluates to false. What's the output? Please check the links😉
26th May 2020, 6:45 AM
Kevin ★
+ 3
'and' aways prints the second value And 'or' prints the first value
26th May 2020, 6:21 AM
Namit Jain
Namit Jain - avatar
+ 2
Yesss, thanks(you opened my eyes) You are correct But this rule will always work with strings right?
26th May 2020, 6:49 AM
Namit Jain
Namit Jain - avatar
+ 2
Got a little, thanka
27th May 2020, 6:24 AM
Shivangi Agarwal
+ 1
Can u pls explain because acc to me 'and' will discard the first one(always) and consider the second one and it's opposite in 'or' For example, print(1 and 2 and 3) Ouput: 3 print(1 and 2 or 3) Ouput: 2 print((1 and 2) or 3) Output: 2 print((1 or 2) and 3) Output: 3
26th May 2020, 6:39 AM
Namit Jain
Namit Jain - avatar