1

I Java it's possible to represent IF-THEN statement in the following form:

a = (x==10) ? true : false;

that is equivalent to

if (x==10)
  a=true;
else
  a=false;

Is it possible to do the same thing in Python?

Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • 3
    You could just do `a = (x == 10)` because the comparison *already produces a boolean*. The term you were looking for is *conditional expression*, which is a *ternary operator* (as opposed to binary operators like `+` or `==`, or unary operators like `-` or `not`) – Martijn Pieters Sep 16 '15 at 08:48
  • see [this question](https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator) – Remi Guan Sep 16 '15 at 08:50

1 Answers1

5
a = True if x == 10 else False

or simply

a = x == 10
gefei
  • 18,922
  • 9
  • 50
  • 67