Python 将布尔值转换为整数的方法

Python 将布尔值转换为整数的方法

Python是一种广泛使用的编程语言,被用于全球各个领域,如Web开发、数据科学、机器学习和自动化处理。布尔值的输出形式是True和False。所以,如果我们想将其转换为整数,可以将true表示为1,false表示为0。在本文中,我们将学习将布尔值转换为整数的不同方法。

将布尔值转换为整数的不同方法

整数函数

在这种方法中,我们将整数函数与布尔值作为参数一起运行,因此我们将自动以整数形式获得输出。让我们看一个例子来更好地理解:

示例

# 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)

输出

上面示例的输出如下:-

1
1

乘法运算

在这种方法中,我们只需要根据输出结果将相应的布尔值与1和0相乘。由于布尔值具有进行数学运算的能力,因此可以轻松执行此操作。让我们看一个例子,以更好地理解:

示例

# 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)

输出

上述示例的输出将如下所示:-

1
1

整数函数和条件表达式

条件值是根据布尔输出返回值的方法。当输出为真时,它会显示一个值,当输出为假时,显示另一个值。让我们通过一个例子来更好地理解它: –

示例

# 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)

输出

以上示例的输出将如下所示:-

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

布尔算术的整数函数

在这个方法中,将对布尔值进行算术运算。让我们来看一个例子以更好地理解它:

示例

# 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)

输出

上述示例的输出将如下:-

1
0

| 运算符

在这个方法中,我们将使用 | 运算符,并根据布尔输出来赋值。让我们看一个例子来更好地理解:

示例

# 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)

输出

上面例子的输出将如下:

1
0

字典映射

我们将在这个方法中创建一个字典,为布尔值分配值。让我们看一个例子来更好地理解它:

示例

# 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)

输出

上述示例的输出将如下所示:-

1
1

结论

可以参考上述文章了解使用Python将布尔值转换为整数的不同方法,并根据方便选择上述任何一种方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程