总结Python中逻辑运算符的使用

yipeiwu_com6年前Python基础

下表列出了所有Python语言支持的逻辑运算符。假设变量a持有10和变量b持有20,则:

2015513121854474.jpg (586×254)

 示例:

试试下面的例子就明白了所有的Python编程语言提供了逻辑运算符:

#!/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

相关文章

Python列表(list)常用操作方法小结

常见列表对象操作方法: list.append(x) 把一个元素添加到链表的结尾,相当于 a[len(a):] = [x] 。 list.extend(L) 将一个给定列表中的所有元素都...

python WindowsError的错误代码详解

WindowsError的错误代码详解 0操作成功完成。 1功能错误。 2系统找不到指定的文件。 3系统找不到指定的路径。 4系统无法打开文件。 5拒绝访问。 6句柄无效。 7存储控制块...

理解生产者消费者模型及在Python编程中的运用实例

理解生产者消费者模型及在Python编程中的运用实例

什么是生产者消费者模型 在 工作中,大家可能会碰到这样一种情况:某个模块负责产生数据,这些数据由另一个模块来负责处理(此处的模块是广义的,可以是类、函数、线程、进程等)。产 生数据的模块...

75条笑死人的知乎神回复,用60行代码就爬完了

75条笑死人的知乎神回复,用60行代码就爬完了

读:知乎神回复都有些什么特点呢?其实爬取知乎神回复很简单,这篇文章我们就来揭晓一下背后的原理。 我们先来观察一下:   大家看出什么规律了么?短小精辟有没有?赞同很多有没有?...

python MySQLdb使用教程详解

python MySQLdb使用教程详解

本文主要内容python MySQLdb数据库批量插入insert,更新update的: 1.python MySQLdb的使用,写了一个基类让其他的sqldb继承这样比较方便,数据库的...