Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Python

Idiom #252 Conditional assignment

Assign to the variable x the string value "a" if calling the function condition returns true, or the value "b" otherwise.

x = 'ba'[condition()]
x = ('b', 'a')[condition()]
x = "a" if condition() else "b"
x := (if condition() then "a" else "b");

New implementation...
< >
programming-idioms.org