In C the shortest and obvious way to avoid conditions is:
int x = 42;
int oneorzero = !!x; /* Will give us a 1 if x is non-zero or 0 if zero */
Is there a similar way in python?
In C the shortest and obvious way to avoid conditions is:
int x = 42;
int oneorzero = !!x; /* Will give us a 1 if x is non-zero or 0 if zero */
Is there a similar way in python?
I lately used something like
oneorzero = 1 if x else 0
which has the benefit that x
is just tested for its truth value and doesn't necessarily need to be 0 for "false".