python逻辑运算免费教程_Python教程-逻辑运算

本文介绍了Python中的逻辑运算符,包括and、or和not。示例代码展示了这些运算符如何影响条件判断,例如当a和b分别为真或假时,如何决定条件表达式的真假。通过理解这些运算符,可以更好地掌握Python中的条件控制结构。
摘要由CSDN通过智能技术生成

Python逻辑运算符:

运算符

描述

示例

and

所谓逻辑与运算符。如果两个操作数都是真的,那么则条件成立。

(a and b) 为 true.

or

所谓逻辑OR运算符。如果有两个操作数都是非零然后再条件变为真。

(a or b) 为 true.

not

所谓逻辑非运算符。用于反转操作数的逻辑状态。如果一个条件为真,则逻辑非运算符将返回false。

not(a and b) 为 false.

#!/usr/bin/python

a = 10

b = 20

c = 0

if ( 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 = 0

if ( 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"

Line 1 - a and b are true

Line 2 - Either a is true or b is true or both are true

Line 3 - Either a is not true or b is not true

Line 4 - Either a is true or b is true or both are true

Line 5 - Either a is not true or b is not true

http://www.yiibai.com/python/python_basic_operators.html

http://www.runoob.com/python/python-operators.html

http://www.cnblogs.com/linjiqin/p/3608541.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值