Priority and detailed usage of not, and, or in python


Preface

(For Xiaobai) What I shared this time is the execution priority of not, and, or in Python, and their specific usage. This article is more detailed, don't spray if you don't like it.


1. The meaning and priority of not, and, or

Object Return result Priority
not x if x is false,then True,else False 1
x and y if x is false,then x,else y 2
x or y if x is false,then y,else x 3
  • Meaning : not is "not"; and is "and"; or is "or" (you can use mathematics to understand)
    1. not True = False or not False = True ( not true is false, if not false is true )
    2. and is one false and then false , two truths are true , two false is false
    3. or is one true is true , two false is false , two truths are true
  • Priority is not> and> or

The code is as follows (example):

x=1 #将x赋值为1
y=0 #将y赋值为0
z=0 #将z赋值为0
print(x or y and not z)
'''
输出结果为
1
'''

Tips: We know that in programming languages ​​"non-zero is true", that is, "0 is False, 1 is True"

  • Since the priority is not>and>or, first execute not z (that is, not 0),
    that is, not 0 = not False =True =1

    The next step is the turn of and, then y and 1 (known y=0) means 0 and 1, which is
    False and True (false and true). We just talked about and, a false is false , so y and 1 = 0 and 1 = False = 0

    The last step according to priority is the turn of or, that is x or 0 (known x=1),
    that is, 1 or 0 =True or Flase =True = 1 (or is "or", one true is true )
    so the output result Is 1

  • Summary: The code operation process is: (Use mathematical symbols to indicate priority )
    {x or [y and (not z)]}


2. How to use not, and, or

1.not

  • if x is false, then True, else False
    if x is False then not x is True, otherwise it is False
    (If x is false, then "not false" is true, otherwise x is true, then not true is false)
  • In programming a Boolean operation , i.e., the return value is True or False

The code is as follows (example):

print(not 0,not 1,not True,not False)
'''
输出结果为:
True False False True
'''

2.and

  • The return result after and operation:
    Calculate the expression from left to right, if all are true, return the last value, if there is false, return the first value.
  • Simple understanding:
    The purpose of and is to find and return the first False (false) or the last True (true) (from left to right) (one false is false, two true is true)

The code is as follows (example):

1. Find and return the first False (false)

print(1 and 2 and 0 and 4 and False)
'''
输出结果为:
0
'''
  • Because and is to find the first False (false), we know that in the computer,
    False is 0, so the output result is 0

2. Find and return the last True (true)

print(1 and 2 and True and 4 and 6)
print(2 and 5 and True and 7 and True)
'''
输出结果为:
6
True
'''
  • Because 1,2, True, 4,6 is not 0, we know nonzero True , it is true , then the output of the last true value , so the output is 6
  • Because 2, 5, True, 7, and True are not 0, they are all true. Similarly, the last true value is output, so the output result is True

3.or

  • The return result after the or operation:
    Calculate the expression from left to right, and return that true value as long as it encounters a true value, and return the last false value if the expression ends without encountering a true value.
  • Simple understanding:
    The purpose of or is to find and return the first True (true) or the last False (false) (from left to right) (one true is true, two false is false)

The code is as follows (example):

1. Find and return the first True (true)

print(0 or 0 or '' or 8 or 1 )
'''
输出结果为:
8
'''

hint:

  • In python, [] or '' or () are empty or None meaning
    empty is also false, i.e., empty = False = False
  • As you can see from the above code, there are two truth values ​​8 and 1, but we need to find the first truth value (True) in the scope, that is, the first one is 8, so the output result is 8.

2. Find and return the last False (false)

print([] or 0 or '' or None or () )
'''
输出结果为:
()
'''

hint:

  • In python, [] or '' or () are empty or None meaning
    empty is also false, i.e., empty = False = False

  • Here you can know that the codes are all false (False), so find the last false (False) and return this value. The last one is () so the output result is ()


to sum up

  1. Priority is not> and> or
  2. not : If x is false, then "non-false" is true, otherwise x is true, then non-true is false
  3. and : find and return the first False or the last True
  4. or : find and return the first True (true) or the last False (false)

If you still don’t understand or want to know more, please click this link: please click me .

Guess you like

Origin blog.csdn.net/m0_51284422/article/details/109441190