Pergunta

I am a relatively newer Python developer, but I come from a Java background. In Java, there is one-line Boolean checker/assigner (for lack of a better term):

int result = (x)?y:z;

I am trying to use a similar approach in Python, but I am not sure whether this structure exists. Is there a way to declare a variable, check whether a condition is true and assign it to one of two values without using if/else?

Foi útil?

Solução

You can use a conditional expression:

result = y if x else z
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top