There are three important steps in the Python logic operation process. We need to pay attention to each of them. Next we will look at how we can better use the three steps of Python logic operations to improve our applications. I hope you will learn more in the future. 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. But Python is flexible in processing this. 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, 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: as long as 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 = 12
- 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 operations. I hope you will have some gains.