# How To Convert A Boolean To An Integer In Python

To convert a boolean to an integer in Python you can use some solutions such as the int() class, map() function, if else condition and Numpy in Python. Please read the article for more detail.

## To Convert a boolean to an integer in Python.

### Using the int() class

Python offers the int() class to convert the specified value into an integer number. This post will use it to convert a boolean to an integer.

Syntax: int(value, base)

Parameter :

•             value: number or string you want to convert into the integer
•             base: default value is 10, a number representing the number format.

Code Example

In this example, We use the int() class to convert a boolean value [True, False] to an integer, and we use the int() class for each function.

Let’s see the code example below.

# Creating values
bool_val = [True, False]

# Printing initial Values
print("Boolean value is: ", bool_val)

# Converting boolean to integer using int() class
toInt = [int(item) for item in bool_val]

# Printing result
print("After converting to integer", toInt)

Output

Boolean value is:  [True, False]
After converting to integer [1, 0]

### Using the if else condition

In a second way, we use the if else condition. You can easily assign a value to the toInt variable 0 if it matches False and 1 if the value is True.

Then call out the append() method to add an element to the list and print out the list

Example

# Creating values
bool_val = [True, False]

# Printing initial Values
print("Boolean value is: ", bool_val)
toInt = []
# Converting boolean to integer
for item in bool_val:
if item == True:
toInt.append(1)
else:
toInt.append(0)

# Printing result
print("After converting to integer", toInt)

Output

Boolean value is:  [True, False]
After converting to integer [1, 0]

### Using NumPy

Next, we can easily use the NumPy with the multiply() method to convert a boolean to an integer in Python.

Example

# Import NumPy
import numpy
# Creating values
bool_val = [True, False]

# Printing initial Values
print("Boolean value is: ", bool_val)

# Converting boolean to integer using multiply() in NumPy
toInt = numpy.multiply(bool_val, 1)

# Printing result
print("After converting to integer", toInt)

Output

Boolean value is:  [True, False]
After converting to integer [1, 0]

### Using the map() function

Finally, we can use the map() function to convert a boolean to an integer. This method will return a map object of the results after applying the given function (int).

Example

# Creating values
bool_val = [True, False]

# Printing initial Values
print("Boolean value is: ", bool_val)

# Converting boolean to an integer using map()
toInt = list(map(int, bool_val))

# Printing result
print("After converting to integer", toInt)


Output

Boolean value is:  [True, False]
After converting to integer [1, 0]

## Summary

In this article, we have introduced two methods to convert a boolean to an integer in Python. But the int() function is straightforward and suitable to convert from a boolean to an integer. Leave your comment here if you have any questions about this article.