Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make multiple assignments using a conditional expression?

Here is the example:

age = 10
reject = False

if age < 10:
    st = 'Kid'
    reject = True

else:
    st='Adult'
    reject = False

Is it possible? Something like:

statement1:statement2 if age < 10 else statement3:statment4

I am still having problems with understanding ternary operator in Python.

like image 339
metazord Avatar asked Feb 06 '26 09:02

metazord


1 Answers

Assignment statements support multiple targets:

>>> age = 10
>>> st, reject = ('Kid', True) if age < 10 else ('Adult', False)
>>> st, reject
('Adult', False)
like image 151
wim Avatar answered Feb 07 '26 23:02

wim