实例说明Python中比较运算符的使用

yipeiwu_com5年前Python基础

下表列出了所有Python语言支持的比较操作符。假设变量a持有10和变量b持有20,则:

2015513120032469.jpg (585×480)

 例如:

试试下面的例子就明白了所有的Python编程语言提供的比较操作符:

#!/usr/bin/python

a = 21
b = 10
c = 0

if ( a == b ):
  print "Line 1 - a is equal to b"
else:
  print "Line 1 - a is not equal to b"

if ( a != b ):
  print "Line 2 - a is not equal to b"
else:
  print "Line 2 - a is equal to b"

if ( a <> b ):
  print "Line 3 - a is not equal to b"
else:
  print "Line 3 - a is equal to b"

if ( a < b ):
  print "Line 4 - a is less than b" 
else:
  print "Line 4 - a is not less than b"

if ( a > b ):
  print "Line 5 - a is greater than b"
else:
  print "Line 5 - a is not greater than b"

a = 5;
b = 20;
if ( a <= b ):
  print "Line 6 - a is either less than or equal to b"
else:
  print "Line 6 - a is neither less than nor equal to b"

if ( b >= a ):
  print "Line 7 - b is either greater than or equal to b"
else:
  print "Line 7 - b is neither greater than nor equal to b"

当执行上面的程序它会产生以下结果:

Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not equal to b
Line 4 - a is not less than b
Line 5 - a is greater than b
Line 6 - a is either less than or equal to b
Line 7 - b is either greater than or equal to b


相关文章

python 同时运行多个程序的实例

start many programs execfile('C:/Dokumente und Einstellungen/schnei17/Desktop/python/zeit/1...

python字典嵌套字典的情况下找到某个key的value详解

最近在用python写接口的测试程序,期间用到解析字典获取某个key的value,由于多个接口返回的字典格式不是固定的并存在多层嵌套的情况。在字典的方法中也没有找到可直接达到目的的方法(...

手写一个python迭代器过程详解

分析 我们都知道一个可迭代对象可以通过iter()可以返回一个迭代器。 如果想要一个对象称为可迭代对象,即可以使用for,那么必须实现__iter __()方法。 在一个类...

PyTorch中Tensor的拼接与拆分的实现

拼接张量:torch.cat() 、torch.stack() torch.cat(inputs, dimension=0) → Tensor 在给定维度上对输入的张量序列 s...

Linux下远程连接Jupyter+pyspark部署教程

博主最近试在服务器上进行spark编程,因此,在开始编程作业之前,要先搭建一个便利的编程环境,这样才能做到舒心地开发。本文主要有以下内容: 1、python多版本管理利器-pythonb...