总结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 实现归并排序算法
Jun 05 Python
在Python的Django框架中创建和使用模版
Jul 15 Python
Python动刷新抢12306火车票的代码(附源码)
Jan 24 Python
Python中的random.uniform()函数教程与实例解析
Mar 02 Python
Python3中的bytes和str类型详解
May 02 Python
使用python的pandas为你的股票绘制趋势图
Jun 26 Python
pip 安装库比较慢的解决方法(国内镜像)
Oct 06 Python
Python with关键字,上下文管理器,@contextmanager文件操作示例
Oct 17 Python
解决django-xadmin列表页filter关联对象搜索问题
Nov 15 Python
python字符串的拼接方法总结
Nov 18 Python
Python sql注入 过滤字符串的非法字符实例
Apr 03 Python
解决python虚拟环境切换无效的问题
Apr 30 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创建桌面快捷方式的实例代码
2014/02/17 PHP
typecho插件编写教程(六):调用接口
2015/05/28 PHP
php str_replace替换指定次数的方法详解
2017/05/05 PHP
animate动画示例(泪奔的小孩)及stop和delay的使用
2013/05/06 Javascript
Javascript学习笔记之函数篇(五) : 构造函数
2014/11/23 Javascript
JS实现自动变化的导航菜单效果代码
2015/09/09 Javascript
探究JavaScript函数式编程的乐趣
2015/12/14 Javascript
基于jquery实现的仿优酷图片轮播特效代码
2016/01/13 Javascript
jQuery实现的指纹扫描效果实例(附演示与demo源码下载)
2016/01/26 Javascript
ionic2 tabs使用 Modal底部tab弹出框
2016/12/30 Javascript
使用vue实现grid-layout功能实例代码
2018/01/05 Javascript
微信小程序之swiper滑动面板用法示例
2018/12/04 Javascript
React降级配置及Ant Design配置详解
2018/12/27 Javascript
Node.js如何优雅的封装一个实用函数的npm包的方法
2019/04/29 Javascript
微信小程序的tab选项卡的实现效果
2019/05/15 Javascript
记录vue做微信自定义分享的一些问题
2019/09/12 Javascript
JS操作字符串转数字的常见方法示例
2019/10/29 Javascript
vue-cli3 热更新配置操作
2020/09/18 Javascript
Python中使用装饰器来优化尾递归的示例
2016/06/18 Python
python 如何快速找出两个电子表中数据的差异
2017/05/26 Python
python编程通过蒙特卡洛法计算定积分详解
2017/12/13 Python
浅析python协程相关概念
2018/01/20 Python
python3中获取文件当前绝对路径的两种方法
2018/04/26 Python
关于Python的一些学习总结
2018/05/25 Python
使用python实现离散时间傅里叶变换的方法
2019/09/02 Python
解决Tensorflow2.0 tf.keras.Model.load_weights() 报错处理问题
2020/06/12 Python
CSS3 3D旋转rotate效果实例介绍
2016/05/03 HTML / CSS
CSS3让登陆面板3D旋转起来
2016/05/03 HTML / CSS
html5的websockets全双工通信详解学习示例
2014/02/26 HTML / CSS
Fabletics官网:美国运动服饰品牌,由好莱坞女演员凯特·哈德森创立
2019/10/19 全球购物
十岁生日同学答谢词
2014/01/19 职场文书
秋天的怀念教学反思
2014/04/28 职场文书
2014年学校国庆主题活动方案
2014/09/16 职场文书
购房协议书范本(无房产证)
2014/10/07 职场文书
2015年事业单位工作总结
2015/04/27 职场文书
Python实现8种常用抽样方法
2021/06/27 Python