DEV Community

Lakshya Tyagi
Lakshya Tyagi

Posted on

What will be the output of given python code?

a, b = 12, 5 if a+b: print('True') else: print('False') 
Enter fullscreen mode Exit fullscreen mode

Write output with explanation in comment

Top comments (7)

Collapse
 
dwd profile image
Dave Cridland • Edited

All these answers are saying True, and that's right, but many of them suggest if an integer is greater than zero it's True, and that's not quite right - in effect, a coercion of an integer to boolean is performing x != 0, so negative numbers also end up as True:

> python3 Python 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> bool(1) True >>> bool(0) False >>> bool(-1) True >>> 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lulzlol231 profile image
Maxim Mosin

'True' because 12+5=17 (int) and bool(int>0) = True

Collapse
 
jobizil profile image
Ugbem Job

Result will be 'True' because by default, all Python Boolean values greater than 0 result to True.

Collapse
 
sambhavjindalgithub profile image
Sambhav-Jindal-github

True as int value is 17 which is greater than 0, and in python all boolean values greater than 0 given true output.

Collapse
 
srikanthdontha profile image
Srikanth Dontha

True

Collapse
 
promikecoder2020 profile image
ProMikeCoder2020

Via the decoupling a becames 12 and b becames 5 thus their joined sum becames 17. Since 17 is a truthy value it passed the if check and thus prints True

Collapse
 
nailspahija profile image
Nail

True