Ways to Convert Boolean Values to Integer in Python


Python is a widely used programming language used for different purposes all over the world like web development, data science, machine learning and to perform various processes with automation. The output of Boolean is in the form of True & False. So, if we want to convert it into integer, we can represent true as 1 and false as 0. In this article we will learn about the different ways to convert Boolean values into Integer.

Different Ways to Convert Boolean Value to Integer

Integer Function

In this method we will run the integer function along with the Boolean values as the arguments, so, automatically we will receive the output in the form of integers. Let's have a look at an example to understand it in a better way: -

Example

# Case 1
boolean_output = True # The output of boolean is provided as input
integer_output = int(boolean_output) # With the help of integer function the boolean value will be converted into integer
print(integer_output)

# Case 2
boolean_output = False # The output of boolean is provided as inputinteger_output = int(boolean_output) # With the help of integer function the boolean value will be converted into integer
print(integer_output) 

Output

The output of the above example will be as follows: -

1
1

Multiplication Operation

In this method we simply multiply the respective Boolean output with 1 and 0 depending upon the output. This operation can be performed easily as the Boolean values have the ability to perform mathematical operations. Let's have a look at an example to understand it in a better manner: -

Example

# Case 1
boolean_output = True # The output of boolean is provided as input
integer_output = boolean_output * 1 # Multiplication operation of the boolean output is carried out 
print(integer_output) 

# Case 2
boolean_output = False # The output of boolean is provided as inputinteger_output = boolean_output * 1 # Multiplication operation of the boolean output is carried out 
print(integer_output) 

Output

The output of the above example will be as follows: -

1
1

Integer Function with Conditional Expression

Conditional value is used to return the value on the basis of the output of Boolean. It shows a different value on true as the output and another value when false is the output. Let's have a look at an example to understand it in a better manner: -

Example

# Case 1
boolean_output = True # The output of boolean is provided as input
integer_output = int(boolean_output) if boolean_output else 0 # A condition is provided to show 1 as output if boolean value is true or else the output will be shown as 0
print(integer_output) 

# Case 2
boolean_output = False # The output of boolean is provided as input
integer_output = int(boolean_output) if boolean_output else 0 # A condition is provided to show 1 as output if boolean value is true or else the output will be shown as 0
print(integer_output) 

Output

The output of the above example will be as follows: -

# Case-1
1 # True value is represented as 1
# Case-2
0 # False Value is represented as 0

Integer Function with Boolean Arithmetic

Arithmetic operations will be performed on Boolean values in this method. Let's have a look at an example to understand it in a better manner: -

Example

# Case 1
boolean_output = True # The output of boolean is provided as input
integer_output = int(boolean_output + 0) # Arithmetic operation is performed on the boolean output to convert it into integer. If the output is true, it will take its value as 1 and perform the operation and if its output is false, it will take its value as 0 and perform the operation
print(integer_output) 

# Case 2
boolean_output = False # The output of boolean is provided as input
integer_output = int(boolean_output + 0) # Arithmetic operation is performed on the boolean output to convert it into integer. If the output is true, it will take its value as 1 and perform the operation and if its output is false, it will take its value as 0 and perform the operation
print(integer_output) 

Output

The output of the above example will be as follows: -

1
0

| Operator

We will use the | operator in this method and assign values as per the Boolean outputs. Let's have a look at an example to understand it in a better manner: -

Example

# Case 1
boolean_output = True # The output of boolean is provided as input
integer_output = boolean_output | 0# The bitwise operator is provided with the value of 0, which will be reversed and displayed in the output in the case of true as the output
print(integer_output) 

# Case 2
boolean_output = False # The output of boolean is provided as input
integer_output = boolean_output | 0 # The bitwise operator is provided with the value of 0, which will be displayed as it is when the boolean value is false
print(integer_output) 

Output

The output of the above example will be as follows: -

1
0

Dictionary Mapping

We will create a dictionary assigning values to the Boolean values in this method. Let's have a look at an example to understand it in a better manner: -

Example

# Case 1
boolean_output = True # The output of boolean is provided as input
boolean_to_integer = {True: 1, False: 0} # We create a dictionary to map the boolean values to the correct integer values
integer_output = boolean_to_integer[boolean_output] # This dictionary is run along with the boolean value function and the respective value is displayed as the output
print(integer_output) 

# Case 2
boolean_output = False # The output of boolean is provided as input
boolean_to_integer = {True: 1, False: 0} # We create a dictionary to map the boolean values to the correct integer valuesinteger_output = boolean_to_integer[boolean_output] # This dictionary is run along with the boolean value function and the respective value is displayed as the output
print(integer_output) 

Output

The output of the above example will be as follows: -

1
1

Conclusion

One can refer the above article to learn about the different methods that can be used to convert Boolean values into integer using python and use any of the above mentioned method as per convenience

Updated on: 07-Aug-2023

551 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements