Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent for ? in Java for Python? [duplicate]

Tags:

java

python

In java, I use the

variable = something == 1 ? 1 : 0

function all the time. Is there an equivalent function in python?

like image 839
Shwiby Avatar asked Feb 20 '15 04:02

Shwiby


2 Answers

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
like image 137
Smashery Avatar answered Sep 21 '22 02:09

Smashery


It is called the 'conditional' in Python:

>>> 'one' if 1 else 'not'
'one'

Covered in PEP308

like image 37
dawg Avatar answered Sep 25 '22 02:09

dawg