Home > Backend Development > Python Tutorial > Python2.x版本中cmp()方法的使用教程

Python2.x版本中cmp()方法的使用教程

WBOY
Release: 2016-06-10 15:12:37
Original
1248 people have browsed it

 cmp()方法返回两个数的差的符号: -1 如果 x y .
语法

以下是cmp()方法的语法:

cmp( x, y )

Copy after login

参数

  • x -- 这是一个数值表达式
  • y -- 这也是一个数值表达式

返回值

此方法如果xy则返回1
例子

下面的例子显示cmp()方法的使用

#!/usr/bin/python

print "cmp(80, 100) : ", cmp(80, 100)
print "cmp(180, 100) : ", cmp(180, 100)
print "cmp(-80, 100) : ", cmp(-80, 100)
print "cmp(80, -100) : ", cmp(80, -100)

Copy after login

当我们运行上面的程序,它会产生以下结果:

cmp(80, 100) : -1
cmp(180, 100) : 1
cmp(-80, 100) : -1
cmp(80, -100) : 1

Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template