Yes, it is possible:
myLang = 'en' if lang == 'en' else 'de' if lang == 'ger' else 'fa'
The true and false expressions of a single conditional expression are just more expressions. You can put another conditional expression in that place.
If it makes it easier to read you can put parentheses around expressions to group them visually. Python doesn't need these, as the conditional expression has a very low operator precedence; only lambda
is lower.
With parentheses it would read:
myLang = 'en' if lang == 'en' else ('de' if lang == 'ger' else 'fa')
There are better ways to map lang
to a two character string however. Using a dictionary, for example:
language_mapping = {'en': 'en', 'ger': 'de'}
myLang = language_mapping.get(lang, 'fa')
would default to 'fa'
unless the lang
value is in the mapping, using the dict.get()
method.