Ways to Convert Boolean Values to Integer in Python

Python is a widely used programming language for web development, data science, machine learning, and automation. Boolean values in Python are represented as True and False. When converting to integers, True becomes 1 and False becomes 0. This article explores different methods to convert Boolean values to integers.

Using the int() Function

The simplest way to convert Boolean values to integers is using Python's built-in int() function ?

# Converting True to integer
boolean_value = True
integer_result = int(boolean_value)
print(f"True converted to integer: {integer_result}")

# Converting False to integer
boolean_value = False
integer_result = int(boolean_value)
print(f"False converted to integer: {integer_result}")
True converted to integer: 1
False converted to integer: 0

Using Multiplication

Boolean values can participate in arithmetic operations. Multiplying by 1 converts them to integers ?

# Using multiplication with 1
boolean_true = True
boolean_false = False

result_true = boolean_true * 1
result_false = boolean_false * 1

print(f"True * 1 = {result_true}")
print(f"False * 1 = {result_false}")
True * 1 = 1
False * 1 = 0

Using Conditional Expression

A conditional expression (ternary operator) can explicitly map Boolean values to integers ?

# Using conditional expression
boolean_value = True
integer_result = 1 if boolean_value else 0
print(f"True using conditional: {integer_result}")

boolean_value = False
integer_result = 1 if boolean_value else 0
print(f"False using conditional: {integer_result}")
True using conditional: 1
False using conditional: 0

Using Boolean Arithmetic

Adding zero to a Boolean value automatically converts it to an integer ?

# Using addition with zero
boolean_true = True
boolean_false = False

result_true = boolean_true + 0
result_false = boolean_false + 0

print(f"True + 0 = {result_true}")
print(f"False + 0 = {result_false}")
True + 0 = 1
False + 0 = 0

Using Bitwise OR Operator

The bitwise OR operator | with zero converts Boolean values to integers ?

# Using bitwise OR with 0
boolean_true = True
boolean_false = False

result_true = boolean_true | 0
result_false = boolean_false | 0

print(f"True | 0 = {result_true}")
print(f"False | 0 = {result_false}")
True | 0 = 1
False | 0 = 0

Using Dictionary Mapping

A dictionary can map Boolean values to their corresponding integers ?

# Using dictionary mapping
bool_to_int = {True: 1, False: 0}

boolean_true = True
boolean_false = False

result_true = bool_to_int[boolean_true]
result_false = bool_to_int[boolean_false]

print(f"True mapped to: {result_true}")
print(f"False mapped to: {result_false}")
True mapped to: 1
False mapped to: 0

Comparison

Method Syntax Best For
int() int(bool_val) Most readable and direct
Multiplication bool_val * 1 Quick arithmetic operations
Addition bool_val + 0 Simple arithmetic conversion
Bitwise OR bool_val | 0 Bitwise operations context
Dictionary mapping[bool_val] Custom mapping logic

Conclusion

The int() function is the most straightforward method for converting Boolean values to integers. Use arithmetic operations for quick conversions within calculations, and dictionary mapping when you need custom conversion logic.

Updated on: 2026-03-27T11:30:29+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements