To convert the boolean value True to 1 in Python, you can use int(). int() converts True to 1 explicitly.
print(int(True))
#Output:
1
You can also convert True to 1 implicitly in the following way.
print(True == 1)
#Output:
True
When working with different variable types in Python, the ability to easily convert between those types can be valuable.
One such situation is if you have boolean variables and want to convert them to integers.
In Python, you can use the int() function to explicitly convert boolean values True and False into 1 and 0, respectively.
Below shows you how you can use int() to convert True to 1 explicitly.
print(int(True))
#Output:
1
If want to implicitly convert True to 1, you can let Python do it for you as True is equal to 1.
print(True == 1)
#Output:
True
Convert False to 0 in Python
If you want to go the other way and convert False into an integer, you can use int().
False is equal to 0, but if you want to do the conversion explicitly, then you can use int() in the following way.
Below shows you how to convert False to 0 in Python.
print(int(False))
#Output:
1
Hopefully this article has been useful for you to learn how to convert True to 1 in Python.
Leave a Reply