总结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 相关文章推荐
利用Django框架中select_related和prefetch_related函数对数据库查询优化
Apr 01 Python
Python类定义和类继承详解
May 08 Python
Python 常用的安装Module方式汇总
May 06 Python
Python如何快速上手? 快速掌握一门新语言的方法
Nov 14 Python
使用Django和Python创建Json response的方法
Mar 26 Python
python实现贪吃蛇小游戏
Mar 21 Python
python中class的定义及使用教程
Sep 18 Python
django 框架实现的用户注册、登录、退出功能示例
Nov 28 Python
Python过滤掉numpy.array中非nan数据实例
Jun 08 Python
python使用Word2Vec进行情感分析解析
Jul 31 Python
浅谈如何使用python抓取网页中的动态数据实现
Aug 17 Python
python中doctest库实例用法
Dec 31 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分多步骤填写发布信息的简单方法实例代码
2012/09/23 PHP
实现PHP多线程异步请求的3种方法
2014/01/17 PHP
php unicode编码和字符串互转的方法
2020/08/12 PHP
php 实现银联商务H5支付的示例代码
2019/10/12 PHP
IE和Firefox在JavaScript应用中的兼容性探讨
2008/04/01 Javascript
jquery 经典动画菜单效果代码
2010/01/26 Javascript
使用jQuery.Validate进行客户端验证(初级篇) 不使用微软验证控件的理由
2010/06/28 Javascript
jquery 漂亮的删除确认和提交无刷新删除示例
2013/11/13 Javascript
jquery select 设置默认选中的示例代码
2014/02/07 Javascript
用box固定长宽实现图片自动轮播js代码
2014/06/09 Javascript
深入理解JavaScript中的并行处理
2016/09/22 Javascript
javascript 中的事件委托详解
2016/10/25 Javascript
使用BootStrap实现标签切换原理解析
2017/03/14 Javascript
jQuery实现简单的计时器功能实例分析
2017/08/29 jQuery
详解如何使用 vue-cli 开发多页应用
2017/12/16 Javascript
vue脚手架及vue-router基本使用
2018/04/09 Javascript
PHP实现基于Redis的MessageQueue队列封装操作示例
2019/02/02 Javascript
nodejs微信开发之授权登录+获取用户信息
2019/03/17 NodeJs
JQuery使用数组遍历跳出each循环
2020/09/01 jQuery
在Python的gevent框架下执行异步的Solr查询的教程
2015/04/16 Python
pyenv命令管理多个Python版本
2017/03/26 Python
Python用imghdr模块识别图片格式实例解析
2018/01/11 Python
python模拟菜刀反弹shell绕过限制【推荐】
2019/06/25 Python
Python StringIO如何在内存中读写str
2020/01/07 Python
Python turtle库的画笔控制说明
2020/06/28 Python
Python配置pip国内镜像源的实现
2020/08/20 Python
Python页面加载的等待方式总结
2021/02/28 Python
宝拉珍选澳大利亚官方购物网站:Paula’s Choice澳大利亚
2016/09/13 全球购物
结构和类有什么异同
2012/07/16 面试题
《晏子使楚》教学反思
2014/02/08 职场文书
捐款倡议书格式范文
2014/05/14 职场文书
建筑工程造价专业自荐信
2014/07/08 职场文书
2015年暑期社会实践活动总结
2015/03/27 职场文书
2015年社区关工委工作总结
2015/04/03 职场文书
美德少年事迹材料(2016推荐版)
2016/02/25 职场文书
APP界面设计技巧和注意事项
2022/04/29 杂记