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
  • 26,085
  • 16
  • 96
  • 138
Tgsmith61591
  • 5,558
  • 3
  • 36
  • 60

1 Answers1

4

You can use a conditional expression:

result = y if x else z
arshajii
  • 118,519
  • 22
  • 219
  • 270