6

In java, I use the

variable = something == 1 ? 1 : 0

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

Vikrant
  • 4,922
  • 16
  • 46
  • 68
Shwiby
  • 111
  • 5

2 Answers2

4

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
Smashery
  • 49,979
  • 30
  • 90
  • 123
3

It is called the 'conditional' in Python:

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

Covered in PEP308

dawg
  • 80,841
  • 17
  • 117
  • 187