📜  理解Python 3 中的布尔逻辑

📅  最后修改于: 2022-05-13 01:54:32.321000             🧑  作者: Mango

理解Python 3 中的布尔逻辑

布尔值是每种编程语言中都存在的简单易用的概念。布尔值表示“真”或“假”的概念。在编写算法或任何程序时,经常会出现我们希望在不同情况下执行不同代码的情况。布尔值帮助我们的代码做到这一点简单而有效。更常见的是,作为某种比较操作的结果返回一个布尔值。
有两个布尔关键字:True 和 False
运算符:运算符是Python中用于执行算术或逻辑计算的特殊符号。要对其进行操作的值称为操作数。而操作由运算符表示(例如 +、-、/、*、% 等)

比较运算符

运算符用于比较值。它在计算条件后返回 True 或 False。

OperatorMeaningExample
>Greater than – True if left operand is greater than the rightx > y
<Less than – True if left operand is less than the right\\\x < y
==Equal to – True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to – True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to – True if left operand is less than or equal to the rightx <= y

逻辑运算符

有三个逻辑运算符:and, or, not

OperatorMeaningExample
andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if operand is false.not x

真值表

真值表是一个小表,它允许我们为逻辑运算符提供结果。
和 Table :它需要两个操作数。

aba and b
FalseFalseFalse
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue

或 Table :它需要两个操作数。

aba or b
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueTrue

not Table :它只需要一个操作数。

anot a
FalseTrue
TrueFalse

示例 1:检查列表是否为空。我们将在 bool()函数中传递列表。当列表为空时,返回 False,如果列表不为空,则返回 True。

Python3
def check_Empty(list_name):
    print(bool(list_name))
 
if __name__ == "__main__":
    # making an empty list
    my_list =[]
 
    # calling our function
    check_Empty(my_list)
 
    # making an non-empty list
    my_list1 =[1, 2, 3]
 
    # calling our function
    check_Empty(my_list1)


Python3
def print_range_numbers(n):
    i = 1
 
    # will execute until condition is True
    while  i <= n:
        print(i)
        i = i + 1
 
if __name__ == "__main__":
    n = 3
 
    # calling our function
    print_range_numbers(n)


Python3
def myFunction() :
    return True
 
if __name__ == "__main__":
    if myFunction():
        # prints YES if myFunction() returns True
        print("YES !")
 
    else:
        # prints NO if myFunction() returns False
        print("NO !")


Python3
def check_greater(num_1, num_2):
    if num_2 > num_1:
 
        # after evaluating condition if it return True
        # then the following line of code get executed
        print("num_2 is greater than num_1")
 
    else:
        print("num_2 is not greater than num_1")
 
if __name__ == "__main__":
    num_1 = 3
    num_2 = 5
 
    # passing it to our function
    check_greater(num_1, num_2)


输出 :

False
True

示例 2:使用 while 循环打印一个数字范围,while 循环将一直运行,直到条件为 True。

Python3

def print_range_numbers(n):
    i = 1
 
    # will execute until condition is True
    while  i <= n:
        print(i)
        i = i + 1
 
if __name__ == "__main__":
    n = 3
 
    # calling our function
    print_range_numbers(n)

输出 :

1
2
3

示例 3:在布尔值的帮助下,我们可以绑定我们的程序。

Python3

def myFunction() :
    return True
 
if __name__ == "__main__":
    if myFunction():
        # prints YES if myFunction() returns True
        print("YES !")
 
    else:
        # prints NO if myFunction() returns False
        print("NO !")

输出 :

YES !

示例 4:在条件的帮助下检查两个数字中的较大者。在布尔值的帮助下,我们可以比较结果并相应地执行

Python3

def check_greater(num_1, num_2):
    if num_2 > num_1:
 
        # after evaluating condition if it return True
        # then the following line of code get executed
        print("num_2 is greater than num_1")
 
    else:
        print("num_2 is not greater than num_1")
 
if __name__ == "__main__":
    num_1 = 3
    num_2 = 5
 
    # passing it to our function
    check_greater(num_1, num_2)

输出 :

num_2 is greater than num_1