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

yipeiwu_com5年前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

相关文章

Django集成celery发送异步邮件实例

Django集成celery发送异步邮件实例

安装依赖 pip install django-celery-beat pip install django-celery-email pip install celery pip...

Python3解释器知识点总结

Python3解释器知识点总结

Python3 解释器 Linux/Unix的系统上,一般默认的 python 版本为 2.x,我们可以将 python3.x 安装在 /usr/local/python3 目录中。...

selenium+python自动化测试环境搭建步骤

selenium+python自动化测试环境搭建步骤

相对于自动化测试工具QTP来说,selenium小巧、免费,而且兼容Google、FireFox、IE多种浏览器,越来越多的人开始使用selenium进行自动化测试。 我是使用的pyth...

Python中sort和sorted函数代码解析

本文研究的主要是Python中sort和sorted函数的相关内容,具体如下。 一、sort函数 sort函数是序列的内部函数 函数原型: L.sort(cmp=None, key=No...

Django ManyToManyField 跨越中间表查询的方法

1、在 django 表中用到了 manytomany 生成了中间表 pyclub_article_column from django.db import models # Cr...