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


Posted in Python onMay 13, 2015

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

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

 示例:

试试下面的例子就明白了所有的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 相关文章推荐
如何运行Python程序的方法
Apr 21 Python
Python里disconnect UDP套接字的方法
Apr 23 Python
Python中的getopt函数使用详解
Jul 28 Python
实现python版本的按任意键继续/退出
Sep 26 Python
解决Django数据库makemigrations有变化但是migrate时未变动问题
May 30 Python
pandas 按照特定顺序输出的实现代码
Jul 10 Python
如何用Python制作微信好友个性签名词云图
Jun 28 Python
python实现根据文件格式分类
Oct 31 Python
python实现输入的数据在地图上生成热力图效果
Dec 06 Python
Python实现自动签到脚本功能
Aug 20 Python
python PIL模块的基本使用
Sep 29 Python
python解压zip包中文乱码解决方法
Nov 27 Python
解析Python中的二进制位运算符
May 13 #Python
整理Python中的赋值运算符
May 13 #Python
实例说明Python中比较运算符的使用
May 13 #Python
举例讲解Python中的算数运算符的用法
May 13 #Python
详细解析Python中的变量的数据类型
May 13 #Python
Python编程入门的一些基本知识
May 13 #Python
python实现字符串和日期相互转换的方法
May 13 #Python
You might like
PHP 采集程序 常用函数
2008/12/18 PHP
php获取远程图片的两种 CURL方式和sockets方式获取远程图片
2011/11/07 PHP
PHP CURL获取返回值的方法
2014/05/04 PHP
php关键字仅替换一次的实现函数
2015/10/29 PHP
浅谈mysql_query()函数的返回值问题
2016/09/05 PHP
解决FLASH需要点击激活的代码
2006/12/20 Javascript
csdn 博客中实现运行代码功能实现
2009/08/29 Javascript
将CKfinder整合进CKEditor3.0的新方法
2010/01/10 Javascript
基于jquery的商品展示放大镜
2010/08/07 Javascript
JS实现一键回顶功能示例代码
2013/10/28 Javascript
借助FileReader实现将文件编码为Base64后通过AJAX上传
2015/12/24 Javascript
jQuery+css实现的换页标签栏效果
2016/01/27 Javascript
jquery实现简单的banner轮播效果【实例】
2016/03/30 Javascript
JS基于正则表达式实现的密码强度验证功能示例
2017/09/21 Javascript
jquery实现动态添加附件功能
2018/10/23 jQuery
详解Vue项目部署遇到的问题及解决方案
2019/01/11 Javascript
js针对图片加载失败的处理方法分析
2019/08/24 Javascript
如何基于filter实现网站整体变灰功能
2020/04/17 Javascript
基于Python和Scikit-Learn的机器学习探索
2017/10/16 Python
Python 记录日志的灵活性和可配置性介绍
2018/02/27 Python
PyTorch上搭建简单神经网络实现回归和分类的示例
2018/04/28 Python
Python模拟百度自动输入搜索功能的实例
2019/02/14 Python
如何用Python制作微信好友个性签名词云图
2019/06/28 Python
python利用re,bs4,requests模块获取股票数据
2019/07/29 Python
python统计字符的个数代码实例
2020/02/07 Python
使用css3制作动感导航条示例
2014/01/26 HTML / CSS
HTML5中外部浏览器唤起微信分享功能的代码
2020/09/15 HTML / CSS
Shopty西班牙:缝纫机在线销售
2018/01/26 全球购物
园林施工员岗位职责
2013/12/11 职场文书
电子商务助理求职自荐信
2014/04/10 职场文书
房地产端午节活动方案
2014/08/24 职场文书
乡镇党委书记个人整改措施
2014/09/15 职场文书
运动会广播稿200字
2014/10/18 职场文书
老干部座谈会主持词
2015/07/03 职场文书
如何用 Python 子进程关闭 Excel 自动化中的弹窗
2021/05/07 Python
用Python selenium实现淘宝抢单机器人
2021/06/18 Python