Summary of the use of logical operators in Python

Source: Internet
Author: User
This article mainly introduces the use of logical operators in Python, which is the basic knowledge in Python learning. For more information, see the following table to list logical operators supported by all Python languages. Assume that variable a holds 10 and variable B holds 20, then:

Example:

The following example shows that all Python programming languages provide logical operators:

#!/usr/bin/pythona = 10b = 20c = 0if ( a and b ):  print "Line 1 - a and b are true"else:  print "Line 1 - Either a is not true or b is not true"if ( a or b ):  print "Line 2 - Either a is true or b is true or both are true"else:  print "Line 2 - Neither a is true nor b is true"a = 0if ( a and b ):  print "Line 3 - a and b are true"else:  print "Line 3 - Either a is not true or b is not true"if ( a or b ):  print "Line 4 - Either a is true or b is true or both are true"else:  print "Line 4 - Neither a is true nor b is true"if not( a and b ):  print "Line 5 - Either a is not true or b is not true"else:  print "Line 5 - a and b are true"

When the above program is executed, it will produce the following results:

Line 1 - a and b are trueLine 2 - Either a is true or b is true or both are trueLine 3 - Either a is not true or b is not trueLine 4 - Either a is true or b is true or both are trueLine 5 - Either a is not true or b is not true

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.