加载中...
  1. Home
  2. Computing & Technology
  3. Python

Discuss in our forum

Comparison Operations

From , former About.com Guide

See More About:

Comparison operations may be applied to any object. Comparison operations receive higher priority than Boolean operations. Comparisons can be chained in series: "x < y <= z" is equivalent to "x < y" and "y <= z" except that "y" is evaluated only once. If "x < y" proves false, "z" is not evaluated at all.

The comparison operations may be summarised as follows:

  • <: less than
  • <= : less than or equal
  • > : greater than
  • >= : greater than or equal
  • == : equal
  • != : not equal; equivalent to <>
  • <> : not equal; equivalent to !=
  • is : object identity
  • is not : negated object identity

Comparisons between objects of different types, except different numeric types and different string types, never return "equal". Some types (e.g., file objects) support only a degenerate notion of comparison where any two objects of that type are non-equal -- equality can only be established by comparing the contents. The <, <=, > and >= operators raise a TypeError when an operand is a complex number.

Instances of a class usually compare as non-equal unless the class defines the __cmp__() method.

Implementation note: Objects of different types (except numbers) are ordered by their type names. Objects of the same type that don't support proper comparison are ordered by their address in memory (RAM).

Two more comparison operations, "in" and "not in", are granted the same syntactic priority but are supported only by sequence types.

Related Searches method implementation implementation note comparison operations object identity proper comparison string types
  1. Home
  2. Computing & Technology
  3. Python
  4. Python Library
  5. Python Modules - Built-In Functions - Comparison Operations

©2011 About.com. All rights reserved. 

A part of The New York Times Company.