Python study Note 3: logical operators

Source: Internet
Author: User

 

Python has three logical operations: and, or, and not. Corresponds to and, Or, rather. Example:

Python has three logical operations: and, or, and not. Corresponds to and, Or, rather.
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. 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. For 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, the Null String is false, and the non-null string is true. The non-zero number is true. The logic operation rule between numbers and strings and between strings is: 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, returns the value of the Left expression 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 the expression is true or false, the return value of the true value expression is false. If both values 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 = 12
12  print test3 or test4 #result = "First"
13  print test2 or test4 #result = "First"
14  print test1 or test4 #result = 12
15  print test4 or test1 #result = "First"
16  print test2 or test3 #result = ''
17  print test3 or test2 #result = 0

 

 

{Online shopping for rebates, new shopping choices}

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.