DEV Community

Discussion on: What will be the output of given python code?

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