总结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交互模式 1.在命令行模式下,可以执行 python 进入 Python 交互式环境,也可以执 行 python hello.py 运行一个.py 文件。 2.在...

Python简单获取网卡名称及其IP地址的方法【基于psutil模块】

本文实例讲述了Python简单获取网卡名称及其IP地址的方法。分享给大家供大家参考,具体如下: windows上想知道网卡和IP地址,可以使用ipconfig命令,在linux上可以使用...

Python的动态重新封装的教程

让我们描绘一下本文的情节:假设您要在本地机器上运行一个进程,而部分程序逻辑却在另一处。让我们特别假设这个程序逻辑会不时更新, 而您运行进程时,希望使用最新的程序逻辑。有许多方法可以满足刚...

pytorch加载自定义网络权重的实现

在将自定义的网络权重加载到网络中时,报错: AttributeError: 'dict' object has no attribute 'seek'. You can only tor...

Python中的time模块与datetime模块用法总结

time模块 time模块是包含各方面对时间操作的函数. 尽管这些常常有效但不是所有方法在任意平台中有效. time用struct_time表示时间 import time # t...