Three application solutions in Python logic operations

Source: Internet
Author: User

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:

 
 
  1. #coding:utf-8  
  2. test1 = 12 
  3. test2 = 0 
  4. print (test1 > test2) and (test1 > 14) #result = False 
  5. print (test1 < test2) or (test1 > -1) #result = True 
  6. print (not test1) #result = False 
  7. 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:

 
 
  1. #coding:utf-8  
  2. test1 = 12 
  3. test2 = 0 
  4. print (test1 and test2) #result = 0 
  5. print (test1 or test2) #result = 12 
  6. print (not test1) #result = Flase 
  7. 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:
 

 
 
  1. #coding:utf-8   
  2. test1 = 12 
  3. test2 = 0   
  4. test3 = ''   
  5. test4 = "First"   
  6. print test1 and test3 #result = ''   
  7. print test3 and test1 #result = ''   
  8. print test1 and test4 #result = "First"   
  9. print test4 and test1 #result = 12 
  10. print test1 or test2 #result = 12 
  11. print test1 or test3 #result = 1212 print test3 or test4 
    #result = "First" 
  12. print test2 or test4 #result = "First" 
  13. print test1 or test4 #result = 12 
  14. print test4 or test1 #result = "First" 
  15. print test2 or test3 #result = '' 
  16. print test3 or test2 #result = 0 

The above describes the Python logic.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.