Python logic has many problems to solve in its operation. Next we will take a detailed look at how to perform the actual operation. In actual use, there are three types: and, or, and not. Corresponds to and, Or, rather. I hope you will have some gains.
Example:
- #coding:utf-8
- test1 = 12
- test2 = 0
- print (test1 > test2) and (test1 > 14) #result = False
- print (test1 < test2) or (test1 > -1) #result = True
- print (not test1) #result = False
- print (not test2) #result = True
Strictly speaking, the operands of logical operators should be boolean expressions. However, Python logic operations are flexible. Even if the operands are numbers, the interpreter regards them as "expressions ". The Boolean value of A number other than 0 is 1 and 0 is 0.
Example:
- #coding:utf-8
- test1 = 12
- test2 = 0
- print (test1 and test2) #result = 0
- print (test1 or test2) #result = 12
- print (not test1) #result = Flase
- print (not test2) #reslut = True
In Python logic operations, the Null String is false, and the non-null string is true. The non-zero number is true.
The logical operation rules between numbers and strings and between strings are as follows:
For the and operator: If the expression on the left is true, the value returned by the entire expression is the value of the expression on the right. Otherwise, the value of the expression on the left is returned. For the or operator, if the expressions on both sides are true, the result of the entire expression is the value of the Left expression.
If it is true or false, return the value of the true value expression. If both are false, such as null and 0, the return value is the value on the right. Null or 0) Example:
- #coding:utf-8
- test1 = 12
- test2 = 0
- test3 = ''
- test4 = "First"
- print test1 and test3 #result = ''
- print test3 and test1 #result = ''
- print test1 and test4 #result = "First"
- print test4 and test1 #result = 12
- print test1 or test2 #result = 12
- print test1 or test3 #result = 1212 print test3 or test4
#result = "First"
- print test2 or test4 #result = "First"
- print test1 or test4 #result = 12
- print test4 or test1 #result = "First"
- print test2 or test3 #result = ''
- print test3 or test2 #result = 0
The above describes the Python logic.