下表列出了所有python语言支持的逻辑运算符。假设变量a持有10和变量b持有20,则:
示例:
试试下面的例子就明白了所有的python编程语言提供了逻辑运算符:
#!/usr/bin/pythona = 10b = 20c = 0if ( a and b ): print line 1 - a and b are trueelse: print line 1 - either a is not true or b is not trueif ( a or b ): print line 2 - either a is true or b is true or both are trueelse: print line 2 - neither a is true nor b is truea = 0if ( a and b ): print line 3 - a and b are trueelse: print line 3 - either a is not true or b is not trueif ( a or b ): print line 4 - either a is true or b is true or both are trueelse: print line 4 - neither a is true nor b is trueif not( a and b ): print line 5 - either a is not true or b is not trueelse: print line 5 - a and b are true
当执行上面的程序它会产生以下结果:
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