-1

Python has an if operator '?' like Java?

Example in Java:

String res = (2 > 1) ? "yes" : "no";
Marco Canora
  • 335
  • 2
  • 15

1 Answers1

3

No, Python doesn't have ?: operator but if/else can be used to achieve the same result on one line:

res = 'yes' if 2 > 1 else 'no'
niemmi
  • 17,113
  • 7
  • 35
  • 42