-1

Python has an if operator '?' like Java?

Example in Java:

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

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
  • 16,078
  • 7
  • 28
  • 35