Python Conditional Variable Setting
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities
--
Chapters
00:00 Python Conditional Variable Setting
00:33 Accepted Answer Score 123
00:45 Answer 2 Score 8
00:57 Answer 3 Score 1
01:12 Answer 4 Score 3
01:20 Thank you
--
Full question
https://stackoverflow.com/questions/8116...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #variables #ifstatement #conditionalstatements
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities
--
Chapters
00:00 Python Conditional Variable Setting
00:33 Accepted Answer Score 123
00:45 Answer 2 Score 8
00:57 Answer 3 Score 1
01:12 Answer 4 Score 3
01:20 Thank you
--
Full question
https://stackoverflow.com/questions/8116...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #variables #ifstatement #conditionalstatements
#avk47
ACCEPTED ANSWER
Score 123
This is the closest thing to what you are looking for:
value = 'Test' if 1 == 1 else 'NoTest'
Otherwise, there isn't much else.
ANSWER 2
Score 8
You can also do:
value = (1 == 1 and 'test') or (2 == 2 and 'testtwo') or 'nope!'
I prefer this way :D
ANSWER 3
Score 3
Less obvious but nice looking term:
value = ('No Test', 'Test')[1 == 1]
print(value) # prints 'Test'
ANSWER 4
Score 1
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