Python Convert Boolean to Int

Last Updated On Thursday 20th Jan 2022

Python convert boolean to int

Using int() method

	boolVal = True

print("Value", boolVal)

# Converting boolean to integer
boolVal = int(boolVal == True)

# Print
print("Result", boolVal)
	
	Value True
Result 1
	

Using numpy

	import numpy
boolVal = numpy.array([True, False])

print("Initial", boolVal)

# Converting boolean to integer
boolVal = numpy.multiply(boolVal, 1)
# Print
print("Result", str(boolVal))
	
	Initial [ True False]
Result [1 0]
	

python boolean to int

Using map()

	boolVal = [True, False]

print("Initial value", boolVal)

# Converting boolean to integer
boolVal = list(map(int, boolVal))

# Print
print("Resultant", str(boolVal))
	
	Initial [True, False]
Result [1, 0]
	

boolean to int python

	boolVal = True

print("Initial", boolVal)

# Converting boolean to integer
if boolVal:
    boolVal = 1
else:
    boolVal = 0
# Print
print("Result", boolVal)
	
	Initial True
Result 1