0

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?

smci
  • 32,567
  • 20
  • 113
  • 146
TayTay
  • 6,882
  • 4
  • 44
  • 65

1 Answers1

5

You can use a conditional expression:

result = y if x else z
arshajii
  • 127,459
  • 24
  • 238
  • 287