In java, I use the
variable = something == 1 ? 1 : 0
function all the time. Is there an equivalent function in python?
In Python, that operator reads slightly differently - more like English. The equivalent to your Java statement in Python would be:
variable = 1 if something == 1 else 0
It is called the 'conditional' in Python:
>>> 'one' if 1 else 'not'
'one'
Covered in PEP308
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With