문제

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?

도움이 되었습니까?

해결책

You can use a conditional expression:

result = y if x else z
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top