总结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中map()与zip()操作方法

对于map()它的原型是:map(function,sequence),就是对序列sequence中每个元素都执行函数function操作。 比如之前的a,b,c = map(int,r...

python Manager 之dict KeyError问题的解决

程序需要多进程见共享内存,使用了Manager的dict。 最初代码如下: from multiprocessing import Process, Manager d = Mana...

python调用百度地图WEB服务API获取地点对应坐标值

本篇博客介绍如何使用Python调用百度地图WEB服务API获取地点对应坐标值,现有一系列结构化地址数据(如:北京市海淀区上地十街十号),目的是获取对应坐标值。 百度地图开发者平台路线规...

python简单图片操作:打开\显示\保存图像方法介绍

python简单图片操作:打开\显示\保存图像方法介绍

一提到数字图像处理,可能大多数人就会想到matlab,但matlab也有自身的缺点: 1、不开源,价格贵 2、软件容量大。一般3G以上,高版本甚至达5G以上。 3、只能做研究,不易转化成...

详解pandas的外部数据导入与常用方法

外部数据导入 导入excel文件 pandas导入excel用read_excel()方法: import pandas as pd excel_file1 = pd.read_...