布尔值运算

简介: 【6月更文挑战第28天】布尔值运算。

布尔与、布尔或、布尔非不仅能对布尔值进行运算,还可以对其他类型进行运算。布尔与为x and y,如果x为True则返回y值,如果x为False则返回x值。布尔或为x or y,如果x为True则返回x值,如果x为False则返回y值。
在了解了逻辑运算符之后编写实例进行验证,在basis目录下新建文件,命名为logic.py,学习Python逻辑运算符的操作,在PyCharm中输入代码。
operand_a = 5
operand_b = 2
operand_c = 0
operand_d = 0

布尔与

operand_d = operand_a and operand_b
print("{} and {} = {}".format(operand_a, operand_b, operand_d))
operand_d = operand_a and operand_c
print("{} and {} = {}".format(operand_a, operand_c, operand_d))
operand_d = operand_c and operand_a
print("{} and {} = {}".format(operand_c, operand_a, operand_d))
operand_d = operand_c and operand_c
print("{} and {} = {}".format(operand_c, operand_c, operand_d))

布尔或

operand_d = operand_a or operand_b
print("{} or {} = {}".format(operand_a, operand_b, operand_d))
operand_d = operand_a or operand_c
print("{} or {} = {}".format(operand_a, operand_c, operand_d))
operand_d = operand_c or operand_a
print("{} or {} = {}".format(operand_c, operand_a, operand_d))
operand_d = operand_c or operand_c
print("{} or {} = {}".format(operand_c, operand_c, operand_d))

布尔非

operand_d = not operand_a
print("not {} = {}".format(operand_a, operand_d))
operand_d = not operand_c
print("not {} = {}".format(operand_c, operand_d))

目录
相关文章
|
1月前
|
存储 编译器 C语言
【表达式求值】整型提升和算术转换
【表达式求值】整型提升和算术转换
26 0
|
29天前
|
Python
布尔值
【6月更文挑战第1天】布尔值。
23 6
|
7月前
|
Linux C语言 C++
操作符&算数转换题
操作符&算数转换题
39 0
|
10月前
|
人工智能 Shell
if 运算表达式
if 运算表达式
38 1
|
11月前
|
C语言 索引
操作符续(整型提升与算术转换)
操作符续(整型提升与算术转换)
60 0
|
C语言 C++
C++——数据类型的运算(运算符的优先级)
C++——数据类型的运算(运算符的优先级)
|
开发者 索引 Python
数值运算
快速学习数值运算
76 0
数值运算
|
存储 C语言
位操作符的一些运算小技巧
位操作符的一些运算小技巧
|
开发者 索引 Python
数值运算|学习笔记
快速学习数值运算
74 0