总结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语法分析之字符串格式化

前序 There should be one - and preferably only one - obvious way to do it. ———— the Zen of Pyt...

python从sqlite读取并显示数据的方法

本文实例讲述了python从sqlite读取并显示数据的方法。分享给大家供大家参考。具体实现方法如下: import cgi, os, sys import sqlite3 as d...

Django 后台获取文件列表 InMemoryUploadedFile的例子

在使用Django项目中,From表单提交了图片集合 var formdata = new FormData(); formdata.append("csrfmiddlewareto...

python 图片验证码代码

下面是一个实战项目的结果。 复制代码 代码如下:#coding: utf-8 import Image,ImageDraw,ImageFont,os,string,random,Imag...

Python3实现配置文件差异对比脚本

Python3实现配置文件差异对比脚本

应用场景:配置文件由于升级改动了,我们想看看升级后的配置文件相对于之前的改动了哪些配置项 注意:这个脚本只能检测的配置文件是键值对的形式,就是key=value的形式 我在网上找了好久没...