Python Conditional Variable Setting Python Conditional Variable Setting python python

Python Conditional Variable Setting


This is the closest thing to what you are looking for:

value = 'Test' if 1 == 1 else 'NoTest'

Otherwise, there isn't much else.


You can also do:

value = (1 == 1 and 'test') or (2 == 2 and 'testtwo') or 'nope!'

I prefer this way :D


value = [1, 2][1 == 1] ;)

...well I guess this would work too:value = ['none true', 'one true', 'both true'][(1 == 1) + (2 == 2)]

Not exactly good programming practice or readable code but amusing and compact, at the very least. Python treats booleans as numbers, so True is 1 and False is 0. [1, 2][True] = 2, [1, 2][False] = 1 and [1, 2, 3][True + True] = 3