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

yipeiwu_com6年前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 实现数字字符串左侧补零的方法

因为做新闻爬虫,url里面0-9的日期要左侧加零。经过查询之后得到了两种方法。 一、先设一个足够大的数,比如1000000,然后加上当前的数字比如9,得到1000009,然后转化为字符串...

下载安装setuptool和pip linux安装pip

复制代码 代码如下:#! /bin/bashfiles_url=(https://pypi.python.org/packages/source/s/setuptools/setupto...

详解Python中的type()方法的使用

 type()方法返回传递变量的类型。如果传递变量是字典那么它将返回一个字典类型。 语法 以下是type()方法的语法: type(dict) 参数  ...

python处理xml文件的方法小结

本文实例讲述了python处理xml文件的方法。分享给大家供大家参考,具体如下: 前一段时间因为工作的需要,学习了一点用Python处理xml文件的方法,现在贴出来,供大家参考。 xml...

Python PyAutoGUI模块控制鼠标和键盘实现自动化任务详解

Python PyAutoGUI模块控制鼠标和键盘实现自动化任务详解

本文实例讲述了Python PyAutoGUI模块控制鼠标和键盘实现自动化任务。分享给大家供大家参考,具体如下: PyAutoGUI是用Python写的一个模块,使用它可以控制鼠标和键盘...