PythonEnthusiast PythonEnthusiast - 1 year ago 127
Python Question

How to convert false to 0 and true to 1 in python

I'm just wondering is there any way in python which converts

of type
to 1 and
of type
to 0.

For eg:-
x = 'true' and type(x) = unicode

I want
x = 1

PS:- I dont want to use if-else.

Answer Source

Use int() on a boolean test:

x = int(x == 'true')

int() turns the boolean into 1 or 0. Note that any value not equal to 'true' will result in 0 being returned.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download