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