总结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

相关文章

Python类属性与实例属性用法分析

本文实例分析了Python类属性与实例属性用法。分享给大家供大家参考。具体如下: 类属性:类名.属性名  实例属性:实例.属性名 >>> class te...

PyQt5每天必学之拖放事件

PyQt5每天必学之拖放事件

在PyQt5教程的这一部分,我们将讨论拖放操作。 在电脑图形用户界面,拖放事件就是点击一个虚拟对象,并将其拖动到其他位置或到另一个虚拟物体的动作。在一般情况下,它可以被用于调用多种动作,...

python redis连接 有序集合去重的代码

python redis连接 有序集合去重的代码如下所述: # -*- coding: utf-8 -*- import redis from constant import re...

Python3.7安装keras和TensorFlow的教程图解

Python3.7安装keras和TensorFlow的教程图解

win10 Python3.7安装keras深度学习集成包 TensorFlow 和Ubuntu下安装keras 在win10下安装 安装时必须检查你的python是否为64位,32位不...

python模拟登录百度代码分享(获取百度贴吧等级)

复制代码 代码如下:# -*- coding: utf8 -*-'''Created on 2013-12-19 @author: good-temper''' import urlli...