Use int for casting a Boolean value in Python. Using int() method will convert Boolean to Int, 1 for True and 0 for False.
Example convert boolean to int python
Simple python example code. Do not use a lowercase boolean value otherwise, it will throw an error. (invalid literal for int() with base 10: ‘true’)
answer = True
print(int(answer))
answer = False
print(int(answer))
Output:
Another way is using if else condition
bool_val = True
print("Initial value", bool_val)
# Converting boolean to integer
if bool_val:
bool_val = 1
else:
bool_val = 0
print("Converted Int value", bool_val)
Output:
Initial value True
Converted Int value 1
Do comment if you have any doubts and another example on this Python Boolean int topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.